source: trunk/packages/vizservers/nanovis/ConvexPolygon.cpp @ 1984

Last change on this file since 1984 was 1984, checked in by gah, 13 years ago

Clean up debugging/printing traces

File size: 5.3 KB
Line 
1 
2/*
3 * ----------------------------------------------------------------------
4 * ConvexPolygon.cpp: convex polygon class
5 *
6 * ======================================================================
7 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
8 *           Purdue Rendering and Perceptualization Lab (PURPL)
9 *
10 *  Copyright (c) 2004-2006  Purdue Research Foundation
11 *
12 *  See the file "license.terms" for information on usage and
13 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 * ======================================================================
15 */
16#include "ConvexPolygon.h"
17#include "Trace.h"
18#include <GL/gl.h>
19#include <assert.h>
20
21ConvexPolygon::ConvexPolygon(){}
22
23ConvexPolygon::ConvexPolygon(VertexVector newvertices){
24    vertices.insert(vertices.begin(), newvertices.begin(), newvertices.end());
25}
26
27// Finds the intersection of the line through
28// p1 and p2 with the given plane, putting the
29// result in intersect.
30//
31// If the line lies in the plane, an arbitrary
32// point on the line is returned.
33//
34// http://astronomy.swin.edu.au/pbourke/geometry/planeline/
35bool
36findIntersection(Vector4 p1, Vector4 p2, Vector4 plane, Vector4 &ret){
37    float a = plane.x;
38    float b = plane.y;
39    float c = plane.z;
40    float d = plane.w;
41
42    p1.perspective_devide();
43    float x1 = p1.x;
44    float y1 = p1.y;
45    float z1 = p1.z;
46
47    p2.perspective_devide();
48    float x2 = p2.x;
49    float y2 = p2.y;
50    float z2 = p2.z;
51
52    float uDenom = a * (x1 - x2) + b * (y1 - y2) + c * (z1 - z2);
53    float uNumer = a * x1 + b * y1 + c * z1 + d;
54
55    if (uDenom == 0){
56        //plane parallel to line
57        ERROR("Unexpected code path: ConvexPolygon.cpp\n");
58        if (uNumer == 0){
59            for (int i = 0; i < 4; i++)
60                ret < p1;
61            return true;
62        }
63        return false;
64    }
65
66    float u = uNumer / uDenom;
67    ret.x = x1 + u * (x2 - x1);
68    ret.y = y1 + u * (y2 - y1);
69    ret.z = z1 + u * (z2 - z1);
70    ret.w = 1;
71    return true;
72}
73
74void
75ConvexPolygon::transform(Mat4x4 mat){
76    VertexVector tmp = vertices;
77    vertices.clear();
78
79    for (unsigned int i = 0; i < tmp.size(); i++) {
80        Vector4 vec = tmp[i];
81        vertices.push_back(mat.transform(vec));
82    }
83}
84
85
86void
87ConvexPolygon::translate(Vector4 shift){
88    VertexVector tmp = vertices;
89    vertices.clear();
90
91    for (unsigned int i = 0; i < tmp.size(); i++) {
92        Vector4 vec = tmp[i];
93        vertices.push_back(vec+shift);
94    }
95}
96
97void
98ConvexPolygon::clip(Plane &clipPlane, bool copy_to_texcoord) {
99    if (vertices.size() == 0) {
100        //ERROR("ConvexPolygon: polygon has no vertices\n"); 
101        return;
102    }
103   
104    VertexVector clippedVerts;
105    clippedVerts.reserve(2 * vertices.size());
106
107    // The algorithm is as follows: for each vertex
108    // in the current poly, check to see which
109    // half space it is in: clipped or retained.
110    // If the previous vertex was in the other
111    // half space, find the intersect of that edge
112    // with the clip plane and add that intersect
113    // point to the new list of vertices.  If the
114    // current vertex is in the retained half-space,
115    // add it to the new list as well.
116
117    Vector4 intersect;
118    Vector4 plane = clipPlane.get_coeffs();
119
120    bool prevRetained = is_retained(vertices[0], plane);
121    if (prevRetained)
122        clippedVerts.push_back(vertices[0]);
123
124    for (unsigned int i = 1; i < vertices.size(); i++) {
125        bool retained = is_retained(vertices[i], plane);
126        if (retained != prevRetained) {
127            bool found = findIntersection(vertices[i - 1], vertices[i],
128                                          plane, intersect);
129            assert(found);
130            clippedVerts.push_back(intersect);
131        }
132        if (retained) {
133            clippedVerts.push_back(vertices[i]);
134        }
135        prevRetained = retained;
136    }
137
138    bool retained = is_retained(vertices[0], plane);
139    if (retained != prevRetained) {
140        bool found = findIntersection(vertices[vertices.size() - 1],
141                                      vertices[0], plane, intersect);
142        assert(found);
143        clippedVerts.push_back(intersect);
144    }
145
146    vertices.clear();
147    vertices.insert(vertices.begin(),
148                    clippedVerts.begin(),
149                    clippedVerts.end());
150
151    if(copy_to_texcoord)
152        copy_vertices_to_texcoords();
153}
154
155void
156ConvexPolygon::copy_vertices_to_texcoords(){
157    if(texcoords.size()>0)
158        texcoords.clear();
159
160    for (unsigned int i=0; i<vertices.size(); i++) {
161        texcoords.push_back(vertices[i]);
162    }
163}
164
165void
166ConvexPolygon::Emit(bool use_texture)
167{
168    if (vertices.size() >= 3) {
169        for (unsigned int i = 0; i<vertices.size(); i++) {
170            if(use_texture) {
171                glTexCoord4fv((float *)&(texcoords[i]));
172                //glTexCoord4fv((float *)&(vertices[i]));
173            }
174            glVertex4fv((float *)&(vertices[i]));
175        }
176    }
177}
178
179
180void
181ConvexPolygon::Emit(bool use_texture, Vector3& shift, Vector3& scale)
182{
183    if (vertices.size() >= 3) {
184        for (unsigned int i = 0; i<vertices.size(); i++) {
185            if(use_texture) {
186                glTexCoord4fv((float *)&(vertices[i]));
187            }
188            Vector4 tmp = (vertices[i]);
189            Vector4 shift_4d = Vector4(shift.x, shift.y, shift.z, 0);
190            tmp = tmp + shift_4d;
191            tmp.x = tmp.x * scale.x;
192            tmp.y = tmp.y * scale.y;
193            tmp.z = tmp.z * scale.z;
194            glVertex4fv((float *)(&tmp));
195        }
196    }
197}
Note: See TracBrowser for help on using the repository browser.