| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
// |
| 38 |
// Code for a Surface of Revolution class. This is not really very |
| 39 |
// robust or well-designed (or complete or...). |
| 40 |
// |
| 41 |
|
| 42 |
#include <Inventor/SbLinear.h> |
| 43 |
#include <Inventor/nodes/SoSeparator.h> |
| 44 |
#include <Inventor/nodes/SoNormal.h> |
| 45 |
#include <Inventor/nodes/SoNormalBinding.h> |
| 46 |
#include <Inventor/nodes/SoShapeHints.h> |
| 47 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 48 |
#include <Inventor/nodes/SoQuadMesh.h> |
| 49 |
|
| 50 |
#include "RevClass.h" |
| 51 |
|
| 52 |
// |
| 53 |
// Utility function used to figure out the normal of a 2D line |
| 54 |
// segment. |
| 55 |
// |
| 56 |
static inline void |
| 57 |
figureNormal(const SbVec3f &p1, const SbVec3f &p2, SbVec3f &result) |
| 58 |
{ |
| 59 |
double dx = p1[0] - p2[0]; |
| 60 |
double dy = p1[1] - p2[1]; |
| 61 |
result.setValue(-dy, dx, 0.0); |
| 62 |
result.normalize(); |
| 63 |
} |
| 64 |
|
| 65 |
// |
| 66 |
// This is used to figure out if the angle between the three given |
| 67 |
// points is greater than the given angle (given as cos(angle)). If |
| 68 |
// it is, code further down will create a sharp (faceted) edge. |
| 69 |
// |
| 70 |
static int |
| 71 |
angleGreater(const SbVec3f &p0, const SbVec3f &p1, const SbVec3f &p2, |
| 72 |
double cosCreaseAngle) |
| 73 |
{ |
| 74 |
SbVec3f v1 = p0 - p1; |
| 75 |
SbVec3f v2 = p1 - p2; |
| 76 |
double cAngle; |
| 77 |
double l1 = v1.length(); |
| 78 |
double l2 = v2.length(); |
| 79 |
if (l1 < 0.001 || l2 < 0.001) cAngle = 0.0; |
| 80 |
else |
| 81 |
{ |
| 82 |
v1 /= l1; v2 /= l2; |
| 83 |
cAngle = v1.dot(v2); |
| 84 |
} |
| 85 |
return cAngle < cosCreaseAngle; |
| 86 |
} |
| 87 |
|
| 88 |
// |
| 89 |
// Given a set of 2D profile coordinates (the z-coordinate of this |
| 90 |
// Vec3f field is ignored), this creates a surface of revolution |
| 91 |
// (including normals). |
| 92 |
// If passed NULL, this routine will use the last coordinates passed |
| 93 |
// (this is used when the number of sides in the revolution is |
| 94 |
// changed). |
| 95 |
// |
| 96 |
void |
| 97 |
RevolutionSurface::createSurface(const SoMFVec3f *prof) |
| 98 |
{ |
| 99 |
static int p_alloc = 0; |
| 100 |
static SbVec3f *profile = NULL; |
| 101 |
static double cca; |
| 102 |
|
| 103 |
// |
| 104 |
// Ok, cheesy hack! |
| 105 |
// First time through, allocate space for profile points, and |
| 106 |
// calculate cosine of crease angle. |
| 107 |
// |
| 108 |
if (p_alloc == 0) |
| 109 |
{ |
| 110 |
p_alloc = 2; |
| 111 |
profile = new SbVec3f[p_alloc*2]; |
| 112 |
cca = cos(creaseAngle); |
| 113 |
} |
| 114 |
|
| 115 |
int nr, nc; |
| 116 |
int i, j; |
| 117 |
|
| 118 |
if (prof != NULL) |
| 119 |
{ |
| 120 |
nc = NumSides; |
| 121 |
nr = 0; |
| 122 |
|
| 123 |
const SoMFVec3f &pc = (*prof); |
| 124 |
int pcn = pc.getNum(); |
| 125 |
if (pcn*2 > p_alloc) |
| 126 |
{ |
| 127 |
// Allocate more space for profile points |
| 128 |
p_alloc = pcn*4; |
| 129 |
delete[] profile; |
| 130 |
profile = new SbVec3f[p_alloc]; |
| 131 |
} |
| 132 |
|
| 133 |
// |
| 134 |
// If the user drew from bottom to top, cool... |
| 135 |
// |
| 136 |
if (pcn > 0 && pc[0][1] < pc[pcn-1][1]) |
| 137 |
{ |
| 138 |
for (j = 0; j < pcn; j++) |
| 139 |
{ |
| 140 |
profile[nr] = pc[j]; |
| 141 |
++nr; |
| 142 |
// |
| 143 |
// If the angle at an interior vertex is too big, we |
| 144 |
// create an extra vertex to get a sharp edge |
| 145 |
// |
| 146 |
if (j > 0 && j < pcn-1 && |
| 147 |
angleGreater(pc[j-1], pc[j], pc[j+1], cca)) |
| 148 |
{ |
| 149 |
profile[nr] = pc[j]; |
| 150 |
++nr; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
// |
| 155 |
// If the user drew profile from top to bottom, gotta reverse |
| 156 |
// |
| 157 |
else |
| 158 |
{ |
| 159 |
for (j = pcn-1; j >= 0; j--) |
| 160 |
{ |
| 161 |
profile[nr] = pc[j]; |
| 162 |
++nr; |
| 163 |
// |
| 164 |
// If the angle at an interior vertex is too big, we |
| 165 |
// create an extra vertex to get a sharp edge |
| 166 |
// |
| 167 |
if (j > 0 && j < pcn-1 && |
| 168 |
angleGreater(pc[j+1], pc[j], pc[j-1], cca)) |
| 169 |
{ |
| 170 |
profile[nr] = pc[j]; |
| 171 |
++nr; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
qmesh->verticesPerRow = nc; |
| 176 |
qmesh->verticesPerColumn = nr; |
| 177 |
} |
| 178 |
else // Profile already figured out |
| 179 |
{ |
| 180 |
nc = NumSides; |
| 181 |
qmesh->verticesPerRow.setValue(nc); |
| 182 |
nr = (int)qmesh->verticesPerColumn.getValue(); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
// Make sure we have enough room in coords/norms: |
| 187 |
coords->point.setNum(nr*nc); |
| 188 |
norms->vector.setNum(nr*nc); |
| 189 |
|
| 190 |
// Grab a pointer to efficiently edit coords/norms: |
| 191 |
SbVec3f *coordPtr = coords->point.startEditing(); |
| 192 |
SbVec3f *normPtr = norms->vector.startEditing(); |
| 193 |
|
| 194 |
// |
| 195 |
// Ok, figure out the first (and last) set of normals |
| 196 |
// |
| 197 |
for (j = 1; j < nr-1; j++) |
| 198 |
{ |
| 199 |
SbVec3f n; |
| 200 |
figureNormal(profile[j-1], profile[j+1], n); |
| 201 |
normPtr[j*nc] = n; |
| 202 |
normPtr[j*nc + nc-1] = n; |
| 203 |
} |
| 204 |
// |
| 205 |
// And figure out the normals at the top and bottom |
| 206 |
// |
| 207 |
if (nr > 1) |
| 208 |
{ |
| 209 |
SbVec3f n; |
| 210 |
figureNormal(profile[0], profile[1], n); |
| 211 |
normPtr[0] = n; |
| 212 |
normPtr[nc-1] = n; |
| 213 |
|
| 214 |
figureNormal(profile[nr-2], profile[nr-1], n); |
| 215 |
normPtr[nr*nc-nc] = n; |
| 216 |
normPtr[nr*nc-1] = n; |
| 217 |
} |
| 218 |
|
| 219 |
// |
| 220 |
// Ok, now that the initial profile and normals have been figured |
| 221 |
// out, just rotate them all about the y axis to get all of the |
| 222 |
// other points... |
| 223 |
// |
| 224 |
for (i = 0; i < nc; i++) { |
| 225 |
double angle = 2.0*M_PI * i / double(nc-1); |
| 226 |
double ca = cos(angle); |
| 227 |
double sa = sin(angle); |
| 228 |
|
| 229 |
// Set coordinates |
| 230 |
for (j = 0; j < nr; j++) { |
| 231 |
coordPtr[j*nc + i] = SbVec3f(profile[j][0]*ca, |
| 232 |
profile[j][1], profile[j][0]*sa); |
| 233 |
} |
| 234 |
// And normals |
| 235 |
if (i != 0 && i != nc-1) for (j = 0; j < nr; j++) { |
| 236 |
normPtr[j*nc + i] = |
| 237 |
SbVec3f(norms->vector[j*nc][0]*ca, |
| 238 |
norms->vector[j*nc][1], norms->vector[j*nc][0]*sa); |
| 239 |
} |
| 240 |
} |
| 241 |
// Let Inventor know we're done modifying these for now: |
| 242 |
coords->point.finishEditing(); |
| 243 |
norms->vector.finishEditing(); |
| 244 |
} |
| 245 |
|
| 246 |
// |
| 247 |
// Construct a new surface of revolution. This sets up the scene |
| 248 |
// graph (which will be returned by getSceneGraph()) |
| 249 |
// |
| 250 |
RevolutionSurface::RevolutionSurface() |
| 251 |
{ |
| 252 |
root = new SoSeparator; |
| 253 |
root->ref(); |
| 254 |
|
| 255 |
// |
| 256 |
// This combination of shape hints will turn on two-sided lighting |
| 257 |
// for us (if it is available) |
| 258 |
// |
| 259 |
SoShapeHints *hints = new SoShapeHints; |
| 260 |
root->addChild(hints); |
| 261 |
hints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE; |
| 262 |
hints->faceType = SoShapeHints::CONVEX; |
| 263 |
|
| 264 |
coords = new SoCoordinate3; |
| 265 |
root->addChild(coords); |
| 266 |
|
| 267 |
norms = new SoNormal; |
| 268 |
root->addChild(norms); |
| 269 |
|
| 270 |
SoNormalBinding *nb = new SoNormalBinding; |
| 271 |
root->addChild(nb); |
| 272 |
nb->value = SoNormalBinding::PER_VERTEX_INDEXED; |
| 273 |
|
| 274 |
qmesh = new SoQuadMesh; |
| 275 |
root->addChild(qmesh); |
| 276 |
|
| 277 |
NumSides = 20; // Default... |
| 278 |
} |
| 279 |
|
| 280 |
// |
| 281 |
// Destructor. Must unref everything reffed in the constructor. |
| 282 |
// |
| 283 |
RevolutionSurface::~RevolutionSurface() |
| 284 |
{ |
| 285 |
root->unref(); |
| 286 |
} |
| 287 |
|
| 288 |
void |
| 289 |
RevolutionSurface::changeNumSides(int n) |
| 290 |
{ |
| 291 |
NumSides = n; |
| 292 |
|
| 293 |
createSurface(NULL); // Re-calculate from old profile |
| 294 |
} |
| 295 |
|
| 296 |
SoSeparator * |
| 297 |
RevolutionSurface::getSceneGraph() |
| 298 |
{ |
| 299 |
return root; |
| 300 |
} |