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

Last change on this file since 3502 was 3502, checked in by ldelgass, 7 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
RevLine 
[2798]1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
[978]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 *
[3502]10 *  Copyright (c) 2004-2013  HUBzero Foundation, LLC
[978]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 */
[2822]16#include <assert.h>
17
[2823]18#include <GL/glew.h>
[2822]19
[978]20#include "ConvexPolygon.h"
[1984]21#include "Trace.h"
[978]22
[3492]23using namespace vrmath;
24
[2822]25ConvexPolygon::ConvexPolygon(VertexVector newvertices)
26{
[978]27    vertices.insert(vertices.begin(), newvertices.begin(), newvertices.end());
28}
29
[2822]30void
[3492]31ConvexPolygon::transform(const Matrix4x4d& mat)
[2822]32{
[978]33    VertexVector tmp = vertices;
34    vertices.clear();
35
36    for (unsigned int i = 0; i < tmp.size(); i++) {
[3492]37        Vector4f vec = tmp[i];
[978]38        vertices.push_back(mat.transform(vec));
39    }
40}
41
[2822]42void
[3492]43ConvexPolygon::translate(const Vector4f& shift)
[2822]44{
[978]45    VertexVector tmp = vertices;
46    vertices.clear();
47
48    for (unsigned int i = 0; i < tmp.size(); i++) {
[3492]49        Vector4f vec = tmp[i];
50        vertices.push_back(vec + shift);
[978]51    }
52}
53
[3362]54#define SIGN_DIFFERS(x, y) \
55    ( ((x) < 0.0 && (y) > 0.0) || ((x) > 0.0 && (y) < 0.0) )
56
57bool
[3492]58ConvexPolygon::clip(nv::Plane& clipPlane, bool copyToTexcoord)
[2822]59{
[978]60    if (vertices.size() == 0) {
[3452]61        ERROR("polygon has no vertices");
[3362]62        return false;
[978]63    }
[3362]64
[978]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
[3362]70    // half space it is in: clipped (outside) or inside.
[978]71    // If the previous vertex was in the other
[3362]72    // half space, find the intersection of that edge
73    // with the clip plane and add that intersection
[978]74    // point to the new list of vertices.  If the
[3362]75    // current vertex is in the inside half-space,
[978]76    // add it to the new list as well.
77
[3492]78    Vector4f plane = clipPlane.getCoeffs();
[978]79
[3362]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]);
[978]90        }
[3362]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            }
[978]101        }
[3362]102        dot1 = dot2;
103        v1 = v2;
[978]104    }
105
106    vertices.clear();
107
[3362]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    }
[978]121}
122
[3362]123#undef SIGN_DIFFERS
124
[2822]125void
[2877]126ConvexPolygon::copyVerticesToTexcoords()
[2822]127{
128    if (texcoords.size() > 0)
[978]129        texcoords.clear();
130
[2822]131    for (unsigned int i = 0; i < vertices.size(); i++) {
[978]132        texcoords.push_back(vertices[i]);
133    }
134}
135
[2822]136void
[2877]137ConvexPolygon::emit(bool useTexture)
[978]138{
139    if (vertices.size() >= 3) {
[2822]140        for (unsigned int i = 0; i < vertices.size(); i++) {
[2877]141            if (useTexture) {
[978]142                glTexCoord4fv((float *)&(texcoords[i]));
143                //glTexCoord4fv((float *)&(vertices[i]));
144            }
145            glVertex4fv((float *)&(vertices[i]));
146        }
[3362]147    } else {
[3452]148        WARN("No polygons to render");
[3362]149    }
[978]150}
151
152void 
[3492]153ConvexPolygon::emit(bool useTexture, const Vector3f& shift, const Vector3f& scale) 
[978]154{
155    if (vertices.size() >= 3) {
[2822]156        for (unsigned int i = 0; i < vertices.size(); i++) {
[2877]157            if (useTexture) {
[978]158                glTexCoord4fv((float *)&(vertices[i]));
159            }
[3492]160            Vector4f tmp = (vertices[i]);
161            Vector4f shift_4d = Vector4f(shift.x, shift.y, shift.z, 0);
[978]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        }
[3362]168    } else {
[3452]169        WARN("No polygons to render");
[978]170    }
171}
Note: See TracBrowser for help on using the repository browser.