Ignore:
Timestamp:
Feb 26, 2013, 1:34:35 PM (12 years ago)
Author:
ldelgass
Message:

Merge nanovis2 branch to trunk

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/packages/vizservers/nanovis/ConvexPolygon.cpp

    r3177 r3362  
    2626}
    2727
    28 // Finds the intersection of the line through
    29 // p1 and p2 with the given plane, putting the
    30 // result in intersect.
    31 //
    32 // If the line lies in the plane, an arbitrary
    33 // point on the line is returned.
    34 //
    35 // http://astronomy.swin.edu.au/pbourke/geometry/planeline/
    36 bool
    37 findIntersection(const Vector4& pt1, const Vector4& pt2, const Vector4& plane, Vector4& ret)
    38 {
    39     float a = plane.x;
    40     float b = plane.y;
    41     float c = plane.z;
    42     float d = plane.w;
    43 
    44     Vector4 p1 = pt1;
    45     p1.perspectiveDivide();
    46     float x1 = p1.x;
    47     float y1 = p1.y;
    48     float z1 = p1.z;
    49 
    50     Vector4 p2 = pt2;
    51     p2.perspectiveDivide();
    52     float x2 = p2.x;
    53     float y2 = p2.y;
    54     float z2 = p2.z;
    55 
    56     float uDenom = a * (x1 - x2) + b * (y1 - y2) + c * (z1 - z2);
    57     float uNumer = a * x1 + b * y1 + c * z1 + d;
    58 
    59     if (uDenom == 0){
    60         //plane parallel to line
    61         ERROR("Unexpected code path: ConvexPolygon.cpp\n");
    62         if (uNumer == 0){
    63             ret = p1;
    64             return true;
    65         }
    66         return false;
    67     }
    68 
    69     float u = uNumer / uDenom;
    70     ret.x = x1 + u * (x2 - x1);
    71     ret.y = y1 + u * (y2 - y1);
    72     ret.z = z1 + u * (z2 - z1);
    73     ret.w = 1;
    74     return true;
    75 }
    76 
    7728void
    7829ConvexPolygon::transform(const Mat4x4& mat)
     
    9950}
    10051
    101 void
     52#define SIGN_DIFFERS(x, y) \
     53    ( ((x) < 0.0 && (y) > 0.0) || ((x) > 0.0 && (y) < 0.0) )
     54
     55bool
    10256ConvexPolygon::clip(Plane& clipPlane, bool copyToTexcoord)
    10357{
    10458    if (vertices.size() == 0) {
    105         //ERROR("ConvexPolygon: polygon has no vertices\n"); 
    106         return;
     59        ERROR("polygon has no vertices\n");
     60        return false;
    10761    }
    108    
     62
    10963    VertexVector clippedVerts;
    11064    clippedVerts.reserve(2 * vertices.size());
     
    11266    // The algorithm is as follows: for each vertex
    11367    // in the current poly, check to see which
    114     // half space it is in: clipped or retained.
     68    // half space it is in: clipped (outside) or inside.
    11569    // If the previous vertex was in the other
    116     // half space, find the intersect of that edge
    117     // with the clip plane and add that intersect
     70    // half space, find the intersection of that edge
     71    // with the clip plane and add that intersection
    11872    // point to the new list of vertices.  If the
    119     // current vertex is in the retained half-space,
     73    // current vertex is in the inside half-space,
    12074    // add it to the new list as well.
    12175
    122     Vector4 intersect;
    12376    Vector4 plane = clipPlane.getCoeffs();
    12477
    125     bool prevRetained = isRetained(vertices[0], plane);
    126     if (prevRetained)
    127         clippedVerts.push_back(vertices[0]);
    128 
    129     for (unsigned int i = 1; i < vertices.size(); i++) {
    130         bool retained = isRetained(vertices[i], plane);
    131         if (retained != prevRetained) {
    132             bool found = findIntersection(vertices[i - 1], vertices[i],
    133                                           plane, intersect);
    134             assert(found);
    135             clippedVerts.push_back(intersect);
     78    // This implementation is based on the Mesa 3D library (MIT license)
     79    int v1 = 0;
     80    // dot product >= 0 on/inside half-space, < 0 outside half-space
     81    float dot1 = vertices[v1] * plane;
     82    for (unsigned int i = 1; i <= vertices.size(); i++) {
     83        int v2 = (i == vertices.size()) ? 0 : i;
     84        float dot2 = vertices[v2] * plane;
     85        if (dot1 >= 0.0) {
     86            // on/inside
     87            clippedVerts.push_back(vertices[v1]);
    13688        }
    137         if (retained) {
    138             clippedVerts.push_back(vertices[i]);
     89        if (SIGN_DIFFERS(dot1, dot2)) {
     90            if (dot1 < 0.0) {
     91                // outside -> inside
     92                double t = dot1 / (dot1 - dot2);
     93                clippedVerts.push_back(vlerp(vertices[v1], vertices[v2], t));
     94            } else {
     95                // inside -> outside
     96                double t = dot2 / (dot2 - dot1);
     97                clippedVerts.push_back(vlerp(vertices[v2], vertices[v1], t));
     98            }
    13999        }
    140         prevRetained = retained;
    141     }
    142 
    143     bool retained = isRetained(vertices[0], plane);
    144     if (retained != prevRetained) {
    145         bool found = findIntersection(vertices[vertices.size() - 1],
    146                                       vertices[0], plane, intersect);
    147         assert(found);
    148         clippedVerts.push_back(intersect);
     100        dot1 = dot2;
     101        v1 = v2;
    149102    }
    150103
    151104    vertices.clear();
    152     vertices.insert(vertices.begin(),
    153                     clippedVerts.begin(),
    154                     clippedVerts.end());
    155105
    156     if (copyToTexcoord)
    157         copyVerticesToTexcoords();
     106    if (clippedVerts.size() < 3) {
     107        texcoords.clear();
     108        return false;
     109    } else {
     110        vertices.insert(vertices.begin(),
     111                        clippedVerts.begin(),
     112                        clippedVerts.end());
     113
     114        if (copyToTexcoord)
     115            copyVerticesToTexcoords();
     116
     117        return true;
     118    }
    158119}
     120
     121#undef SIGN_DIFFERS
    159122
    160123void
     
    180143            glVertex4fv((float *)&(vertices[i]));
    181144        }
    182     }
     145    } else {
     146        WARN("No polygons to render\n");
     147    }
    183148}
    184149
     
    199164            glVertex4fv((float *)(&tmp));
    200165        }
     166    } else {
     167        WARN("No polygons to render\n");
    201168    }
    202169}
Note: See TracChangeset for help on using the changeset viewer.