source: nanovis/tags/1.1.3/ConvexPolygon.cpp @ 6369

Last change on this file since 6369 was 3502, checked in by ldelgass, 11 years ago

Add basic VTK structured points reader to nanovis, update copyright dates.

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
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-2013  HUBzero Foundation, LLC
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 <assert.h>
17
18#include <GL/glew.h>
19
20#include "ConvexPolygon.h"
21#include "Trace.h"
22
23using namespace vrmath;
24
25ConvexPolygon::ConvexPolygon(VertexVector newvertices)
26{
27    vertices.insert(vertices.begin(), newvertices.begin(), newvertices.end());
28}
29
30void
31ConvexPolygon::transform(const Matrix4x4d& mat)
32{
33    VertexVector tmp = vertices;
34    vertices.clear();
35
36    for (unsigned int i = 0; i < tmp.size(); i++) {
37        Vector4f vec = tmp[i];
38        vertices.push_back(mat.transform(vec));
39    }
40}
41
42void
43ConvexPolygon::translate(const Vector4f& shift)
44{
45    VertexVector tmp = vertices;
46    vertices.clear();
47
48    for (unsigned int i = 0; i < tmp.size(); i++) {
49        Vector4f vec = tmp[i];
50        vertices.push_back(vec + shift);
51    }
52}
53
54#define SIGN_DIFFERS(x, y) \
55    ( ((x) < 0.0 && (y) > 0.0) || ((x) > 0.0 && (y) < 0.0) )
56
57bool
58ConvexPolygon::clip(nv::Plane& clipPlane, bool copyToTexcoord)
59{
60    if (vertices.size() == 0) {
61        ERROR("polygon has no vertices");
62        return false;
63    }
64
65    VertexVector clippedVerts;
66    clippedVerts.reserve(2 * vertices.size());
67
68    // The algorithm is as follows: for each vertex
69    // in the current poly, check to see which
70    // half space it is in: clipped (outside) or inside.
71    // If the previous vertex was in the other
72    // half space, find the intersection of that edge
73    // with the clip plane and add that intersection
74    // point to the new list of vertices.  If the
75    // current vertex is in the inside half-space,
76    // add it to the new list as well.
77
78    Vector4f plane = clipPlane.getCoeffs();
79
80    // This implementation is based on the Mesa 3D library (MIT license)
81    int v1 = 0;
82    // dot product >= 0 on/inside half-space, < 0 outside half-space
83    float dot1 = vertices[v1] * plane;
84    for (unsigned int i = 1; i <= vertices.size(); i++) {
85        int v2 = (i == vertices.size()) ? 0 : i;
86        float dot2 = vertices[v2] * plane;
87        if (dot1 >= 0.0) {
88            // on/inside
89            clippedVerts.push_back(vertices[v1]);
90        }
91        if (SIGN_DIFFERS(dot1, dot2)) {
92            if (dot1 < 0.0) {
93                // outside -> inside
94                double t = dot1 / (dot1 - dot2);
95                clippedVerts.push_back(vlerp(vertices[v1], vertices[v2], t));
96            } else {
97                // inside -> outside
98                double t = dot2 / (dot2 - dot1);
99                clippedVerts.push_back(vlerp(vertices[v2], vertices[v1], t));
100            }
101        }
102        dot1 = dot2;
103        v1 = v2;
104    }
105
106    vertices.clear();
107
108    if (clippedVerts.size() < 3) {
109        texcoords.clear();
110        return false;
111    } else {
112        vertices.insert(vertices.begin(),
113                        clippedVerts.begin(),
114                        clippedVerts.end());
115
116        if (copyToTexcoord)
117            copyVerticesToTexcoords();
118
119        return true;
120    }
121}
122
123#undef SIGN_DIFFERS
124
125void
126ConvexPolygon::copyVerticesToTexcoords()
127{
128    if (texcoords.size() > 0)
129        texcoords.clear();
130
131    for (unsigned int i = 0; i < vertices.size(); i++) {
132        texcoords.push_back(vertices[i]);
133    }
134}
135
136void
137ConvexPolygon::emit(bool useTexture)
138{
139    if (vertices.size() >= 3) {
140        for (unsigned int i = 0; i < vertices.size(); i++) {
141            if (useTexture) {
142                glTexCoord4fv((float *)&(texcoords[i]));
143                //glTexCoord4fv((float *)&(vertices[i]));
144            }
145            glVertex4fv((float *)&(vertices[i]));
146        }
147    } else {
148        WARN("No polygons to render");
149    }
150}
151
152void
153ConvexPolygon::emit(bool useTexture, const Vector3f& shift, const Vector3f& scale)
154{
155    if (vertices.size() >= 3) {
156        for (unsigned int i = 0; i < vertices.size(); i++) {
157            if (useTexture) {
158                glTexCoord4fv((float *)&(vertices[i]));
159            }
160            Vector4f tmp = (vertices[i]);
161            Vector4f shift_4d = Vector4f(shift.x, shift.y, shift.z, 0);
162            tmp = tmp + shift_4d;
163            tmp.x = tmp.x * scale.x;
164            tmp.y = tmp.y * scale.y;
165            tmp.z = tmp.z * scale.z;
166            glVertex4fv((float *)(&tmp));
167        }
168    } else {
169        WARN("No polygons to render");
170    }
171}
Note: See TracBrowser for help on using the repository browser.