Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //поиск ближайшей точки на отрезке
- void GetClosestPoint(double Ax, double Ay, double Bx, double By, double Px, double Py, double& min_distance, MyPoint *nearest)
- {
- double APx = Px - Ax;
- double APy = Py - Ay;
- double ABx = Bx - Ax;
- double ABy = By - Ay;
- double magAB2 = ABx*ABx + ABy*ABy;
- double ABdotAP = ABx*APx + ABy*APy;
- double t = ABdotAP / magAB2;
- double xret, yret;
- if (t < 0)
- {
- xret = Ax;
- yret = Ay;
- }
- else if (t > 1)
- {
- xret = Bx;
- yret = By;
- }
- else
- {
- xret = Ax + ABx*t;
- yret = Ay + ABy*t;
- }
- double distance = pow((Px - xret), 2) + pow((Py - yret), 2);
- if (min_distance > distance)
- {
- min_distance = distance;
- nearest->x = xret;
- nearest->y = yret;
- }
- }
- float clip(float n, float lower, float upper) {
- return std::max(lower, std::min(n, upper));
- }
- bool objectTypeCheck(std::vector<wxString> &types, PI_S57Obj *top) {
- for (const wxString& type : types){
- if (strcmp(top->FeatureName, type) == 0) return true;
- }
- return false;
- }
- bool ChartS63::FindClosestObjectPoint(float lat, float lon, float select_radius,
- PlugIn_ViewPort *VPoint, double *retLat, double *retLon, std::vector<wxString> &types){
- MyPoint nearest{ 0, 0 };
- PI_S57Obj *top;
- double min_distance = 1000000000;
- for (int i = 0; i < PRIO_NUM; ++i) {
- int point_type = 3;
- top = razRules[i][point_type];
- while (top != NULL){
- if (objectTypeCheck(types, top)){
- if (top->pPolyTessGeo) {
- PolyTriGroup *ppg = ((PolyTessGeo *)top->pPolyTessGeo)->Get_PolyTriGroup_head();
- TriPrim *pTP = ppg->tri_prim_head;
- double easting, northing;
- toSM_Plugin(lat, lon, m_ref_lat, m_ref_lon, &easting, &northing);
- //if (isObjectInRadius(easting, northing, top, 10)){
- MyPoint pvert_list[3];
- while (pTP) {
- if (ppg->data_type == DATA_TYPE_DOUBLE) {
- double *p_vertex = pTP->p_vertex;
- switch (pTP->type){
- //каждый полигон разбиваем на отрезки и находим ближайшую точку а нем
- case PTG_TRIANGLE_FAN: {
- for (int it = 0; it < pTP->nVert - 2; it++) {
- GetClosestPoint(p_vertex[0], p_vertex[1], p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[0], p_vertex[1], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- }
- break;
- }
- case PTG_TRIANGLE_STRIP: {
- for (int it = 0; it < pTP->nVert - 2; it++) {
- GetClosestPoint(p_vertex[(it * 2)], p_vertex[(it * 2) + 1], p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[(it * 2)], p_vertex[(it * 2) + 1], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- }
- break;
- }
- case PTG_TRIANGLES: {
- for (int it = 0; it < pTP->nVert; it += 3) {
- GetClosestPoint(p_vertex[(it * 2)], p_vertex[(it * 2) + 1], p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[(it * 2)], p_vertex[(it * 2) + 1], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- }
- break;
- }
- }
- } // double
- else{
- float *p_vertex = (float *)pTP->p_vertex;
- switch (pTP->type){
- case PTG_TRIANGLE_FAN: {
- for (int it = 0; it < pTP->nVert - 2; it++) {
- GetClosestPoint(p_vertex[0], p_vertex[1], p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[0], p_vertex[1], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- }
- break;
- }
- case PTG_TRIANGLE_STRIP: {
- for (int it = 0; it < pTP->nVert - 2; it++) {
- GetClosestPoint(p_vertex[(it * 2)], p_vertex[(it * 2) + 1], p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[(it * 2)], p_vertex[(it * 2) + 1], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- }
- break;
- }
- case PTG_TRIANGLES: {
- for (int it = 0; it < pTP->nVert; it += 3) {
- GetClosestPoint(p_vertex[(it * 2)], p_vertex[(it * 2) + 1], p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[(it * 2) + 2], p_vertex[(it * 2) + 3], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- GetClosestPoint(p_vertex[(it * 2)], p_vertex[(it * 2) + 1], p_vertex[(it * 2) + 4], p_vertex[(it * 2) + 5], easting, northing, min_distance, &nearest);
- }
- break;
- }
- }
- }// float
- pTP = pTP->p_next;
- }
- //}
- }
- }
- top = top->next;
- }
- }
- if (nearest.x==0) return false;
- fromSM_Plugin(nearest.x, nearest.y, m_ref_lat, m_ref_lon, retLat, retLon);
- return true;
- }
Add Comment
Please, Sign In to add comment