zclllyybb commented on code in PR #48695:
URL: https://github.com/apache/doris/pull/48695#discussion_r2014550405


##########
be/src/geo/geo_types.cpp:
##########
@@ -75,6 +80,171 @@ static inline GeoParseStatus to_s2point(double lng, double 
lat, S2Point* point)
     return GEO_PARSE_OK;
 }
 
+double ProjectDistance(const S2Point& point, const S2Point& lineStart, const 
S2Point& lineEnd) {
+    S2Point lineVector = lineEnd - lineStart;
+    S2Point pointVector = point - lineStart;
+    double lineVectorMagnitudeSquared = lineVector.DotProd(lineVector);
+    double t = pointVector.DotProd(lineVector) / lineVectorMagnitudeSquared;
+    t = t > 0 ? t : 0;
+    t = t < 1 ? t : 1;
+    S2Point nearestPoint = lineStart + t * lineVector;
+    S2Point distanceVector = point - nearestPoint;
+    return sqrt(distanceVector.DotProd(distanceVector));
+}
+
+double ComputeDistanceToLine(const S2Point& point, const S2Polyline* line) {
+    const S2Point& line_point1 = line->vertex(0);
+    const S2Point& line_point2 = line->vertex(1);
+    S2LatLng lp1 = S2LatLng(line_point1);
+    S2LatLng lp2 = S2LatLng(line_point2);
+
+    S2LatLng lquery = S2LatLng(point);
+    double lat1 = lp1.lat().degrees();
+    double long1 = lp1.lng().degrees();
+
+    double lat2 = lp2.lat().degrees();
+    double long2 = lp2.lng().degrees();
+
+    double latq = lquery.lat().degrees();
+    double longq = lquery.lng().degrees();
+    return ProjectDistance({latq, longq, 0}, {lat1, long1, 0}, {lat2, long2, 
0});
+}
+
+double ComputeDistanceToLine(const S2Point& point, const S2Point& line_point1,
+                             const S2Point& line_point2) {
+    S2LatLng lp1 = S2LatLng(line_point1);
+    S2LatLng lp2 = S2LatLng(line_point2);
+
+    S2LatLng lquery = S2LatLng(point);
+    double lat1 = lp1.lat().degrees();
+    double long1 = lp1.lng().degrees();
+
+    double lat2 = lp2.lat().degrees();
+    double long2 = lp2.lng().degrees();
+
+    double latq = lquery.lat().degrees();
+    double longq = lquery.lng().degrees();
+    return ProjectDistance({latq, longq, 0}, {lat1, long1, 0}, {lat2, long2, 
0});
+}
+
+double CrossProduct(const S2Point& a, const S2Point& b, const S2Point& c) {
+    return (b.x() - a.x()) * (c.y() - a.y()) - (b.y() - a.y()) * (c.x() - 
a.x());
+}
+
+bool IsPointOnSegment(const S2Point& p, const S2Point& a, const S2Point& b) {
+    return (p.x() >= std::min(a.x(), b.x()) && p.x() <= std::max(a.x(), b.x()) 
&&
+            p.y() >= std::min(a.y(), b.y()) && p.y() <= std::max(a.y(), b.y()) 
&&
+            p.z() >= std::min(a.z(), b.z()) && p.z() <= std::max(a.z(), 
b.z()));
+}
+
+bool DoSegmentsIntersect(const S2Point& a1, const S2Point& a2, const S2Point& 
b1,
+                         const S2Point& b2) {
+    if (std::max(a1.x(), a2.x()) < std::min(b1.x(), b2.x()) ||
+        std::max(a1.y(), a2.y()) < std::min(b1.y(), b2.y()) ||
+        std::max(a1.z(), a2.z()) < std::min(b1.z(), b2.z()) ||
+        std::min(a1.x(), a2.x()) > std::max(b1.x(), b2.x()) ||
+        std::min(a1.y(), a2.y()) > std::max(b1.y(), b2.y()) ||
+        std::min(a1.z(), a2.z()) > std::max(b1.z(), b2.z())) {
+        return false;
+    }
+
+    double d1 = CrossProduct(b1, b2, a1);
+    double d2 = CrossProduct(b1, b2, a2);
+    double d3 = CrossProduct(a1, a2, b1);
+    double d4 = CrossProduct(a1, a2, b2);
+
+    if ((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) {
+        if ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)) {
+            return true;
+        }
+    }
+
+    if (d1 == 0 && IsPointOnSegment(a1, b1, b2)) {
+        return true;
+    }
+    if (d2 == 0 && IsPointOnSegment(a2, b1, b2)) {
+        return true;
+    }
+    if (d3 == 0 && IsPointOnSegment(b1, a1, a2)) {
+        return true;
+    }
+    if (d4 == 0 && IsPointOnSegment(b2, a1, a2)) {
+        return true;
+    }
+
+    return false;
+}
+
+bool IsSegmentsIntersect(const S2Point& point1, const S2Point& point2, const 
S2Point& line_point1,

Review Comment:
   **all** non-const variable or function should be named with underscore 
naming convention.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to