1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
#include <Inventor/SbClip.h>
#include <Inventor/SbVec2f.h>
#include <Inventor/SbPlane.h>
#include <cstdio>
#include <cassert>
/*!
\class SbClip SbClip.h include/Inventor/base/SbClip.h
\brief The SbClip class is a generic polygon clipper class.
\ingroup base
It is used by first adding all vertices in the polygon, and then
clipping against any number of planes. If you need to supply
additional information per vertex (e.g. texture coordinates), you
should supply a callback in the constructor, and a pointer to your
vertex structure in addVertex(). For every new vertex created, the
callback is called with the line being clipped, including the
pointers to your vertex structures and the position of the new
(clipped against some plane) vertex. You should then create a new
vertex structure, calculate your data (e.g. a new texture
coordinate) and return a pointer to this structure.
\COIN_CLASS_EXTENSION
\since Coin 2.0
*/
// FIXME: a small usage example would do wonders for the class
// doc. 20020120 mortene.
/*!
A constructor. Supply a callback if you need to handle additional
data per vertex.
*/
SbClip::SbClip(SbClipCallback * callback, void * userdata)
: callback(callback),
cbdata(userdata),
curr(0)
{
}
/*!
Adds a polygon vertex. \a vdata could be a pointer to your
vertex structure.
*/
void
SbClip::addVertex(const SbVec3f &v, void * vdata)
{
this->array[this->curr].append(SbClipData(v, vdata));
}
/*!
Resets the clipper. This should be called before adding any vertices
when reusing an SbClip instance.
*/
void
SbClip::reset(void)
{
this->array[0].truncate(0);
this->array[1].truncate(0);
}
//
// private method, inline only inside this class
//
inline void
SbClip::outputVertex(const SbVec3f & v, void * data)
{
this->array[this->curr ^ 1].append(SbClipData(v, data));
}
/*!
Clip polygon against \a plane. This might change the number of
vertices in the polygon. For each time a new vertex is created, the
callback supplied in the constructor (if != NULL) is called with the
line being clipped and the new vertex calculated. The callback
should return a new void pointer to be stored by the clipper.
*/
void
SbClip::clip(const SbPlane & plane)
{
int n = this->array[this->curr].getLength();
if (n == 0) return;
// create loop
SbClipData dummy = this->array[this->curr][0];
this->array[this->curr].append(dummy);
const SbVec3f & planeN = plane.getNormal();
for (int i = 0; i < n; i++) {
SbVec3f v0, v1;
void * data0, *data1;
this->array[this->curr][i].get(v0, data0);
this->array[this->curr][i+1].get(v1, data1);
float d0 = plane.getDistance(v0);
float d1 = plane.getDistance(v1);
if (d0 >= 0.0f && d1 < 0.0f) { // exit plane
SbVec3f dir = v1-v0;
// we know that v0 != v1 since we got here
(void) dir.normalize();
float dot = dir.dot(planeN);
SbVec3f newvertex = v0 - dir * (d0/dot);
void * newdata = NULL;
if (this->callback) {
newdata = this->callback(v0, data0, v1, data1, newvertex, this->cbdata);
}
outputVertex(newvertex, newdata);
}
else if (d0 < 0.0f && d1 >= 0.0f) { // enter plane
SbVec3f dir = v1-v0;
// we know that v0 != v1 since we got here
(void) dir.normalize();
float dot = dir.dot(planeN);
SbVec3f newvertex = v0 - dir * (d0/dot);
void * newdata = NULL;
if (this->callback) {
newdata = this->callback(v0, data0, v1, data1, newvertex, this->cbdata);
}
outputVertex(newvertex, newdata);
outputVertex(v1, data1);
}
else if (d0 >= 0.0f && d1 >= 0.0f) { // in plane
outputVertex(v1, data1);
}
}
this->array[this->curr].truncate(0);
this->curr ^= 1;
}
/*!
Returns the number of vertices in the polygon.
\sa SbClip::getVertex()
*/
int
SbClip::getNumVertices(void) const
{
return this->array[this->curr].getLength();
}
/*!
Returns the vertex at index \a idx.
\sa SbClip::getNumVertices()
*/
void
SbClip::getVertex(const int idx, SbVec3f & v, void ** vdata) const
{
v = this->array[this->curr][idx].vertex;
if (vdata) *vdata = this->array[this->curr][idx].data;
}
/*!
Return the vertex data at index \a idx.
*/
void *
SbClip::getVertexData(const int idx) const
{
return this->array[this->curr][idx].data;
}
|