| 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 | * Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 39 | * |
| 40 | _______________________________________________________________________ |
| 41 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 | | |
| 43 | | $Revision $ |
| 44 | | |
| 45 | | Classes: |
| 46 | | SoClipPlaneElement |
| 47 | | |
| 48 | | Author(s) : Paul S. Strauss |
| 49 | | |
| 50 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 | _______________________________________________________________________ |
| 52 | */ |
| 53 | |
| 54 | #include <Inventor/elements/SoClipPlaneElement.h> |
| 55 | #include <Inventor/elements/SoModelMatrixElement.h> |
| 56 | #include <Inventor/errors/SoDebugError.h> |
| 57 | |
| 58 | SO_ELEMENT_SOURCE(SoClipPlaneElement); |
| 59 | |
| 60 | // |
| 61 | // This structure contains all information needed for a clipping |
| 62 | // plane. Pointers to instances of this structure are stored in the |
| 63 | // "planes" SbPList. |
| 64 | // |
| 65 | |
| 66 | struct So_ClipPlane { |
| 67 | SbPlane objPlane; // World-space clipping plane |
| 68 | SbPlane worldPlane; // World-space clipping plane |
| 69 | SbMatrix objToWorld; // Converts object space to world space |
| 70 | SbBool worldPlaneValid;// TRUE if worldPlane was computed |
| 71 | }; |
| 72 | |
| 73 | //////////////////////////////////////////////////////////////////////// |
| 74 | // |
| 75 | // Description: |
| 76 | // Destructor. |
| 77 | // |
| 78 | // Use: private |
| 79 | |
| 80 | SoClipPlaneElement::~SoClipPlaneElement() |
| 81 | // |
| 82 | //////////////////////////////////////////////////////////////////////// |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | //////////////////////////////////////////////////////////////////////// |
| 87 | // |
| 88 | // Description: |
| 89 | // Initializes element. |
| 90 | // |
| 91 | // Use: public |
| 92 | |
| 93 | void |
| 94 | SoClipPlaneElement::init(SoState *) |
| 95 | // |
| 96 | //////////////////////////////////////////////////////////////////////// |
| 97 | { |
| 98 | planes.truncate(0); |
| 99 | startIndex = 0; |
| 100 | } |
| 101 | |
| 102 | //////////////////////////////////////////////////////////////////////// |
| 103 | // |
| 104 | // Description: |
| 105 | // Adds a clip plane to the current set in the state. |
| 106 | // |
| 107 | // Use: public, static |
| 108 | |
| 109 | void |
| 110 | SoClipPlaneElement::add(SoState *state, SoNode *node, const SbPlane &plane) |
| 111 | // |
| 112 | //////////////////////////////////////////////////////////////////////// |
| 113 | { |
| 114 | SoClipPlaneElement *elt; |
| 115 | |
| 116 | // Get an instance we can change (pushing if necessary) |
| 117 | elt = (SoClipPlaneElement *) getElement(state, classStackIndex); |
| 118 | |
| 119 | if (elt != NULL) { |
| 120 | elt->addToElt(plane, SoModelMatrixElement::get(state)); |
| 121 | |
| 122 | // Update node id list in element |
| 123 | elt->addNodeId(node); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | //////////////////////////////////////////////////////////////////////// |
| 128 | // |
| 129 | // Description: |
| 130 | // Override push to copy the existing planes from the previous |
| 131 | // element. |
| 132 | // |
| 133 | // Use: public |
| 134 | |
| 135 | void |
| 136 | SoClipPlaneElement::push(SoState *) |
| 137 | // |
| 138 | //////////////////////////////////////////////////////////////////////// |
| 139 | { |
| 140 | const SoClipPlaneElement *elt = |
| 141 | (const SoClipPlaneElement *) getNextInStack(); |
| 142 | |
| 143 | // Use SbPList::operator = to copy the pointers to the existing |
| 144 | // planes. Since the previous element can't be destroyed before |
| 145 | // this one is, there is no problem with using pointers to |
| 146 | // existing plane structures. However, any new ones we add have to |
| 147 | // be freed up when this instance goes away, so we save the |
| 148 | // starting index to allow us to fix this in pop(). |
| 149 | planes = elt->planes; |
| 150 | startIndex = planes.getLength(); |
| 151 | nodeIds = elt->nodeIds; |
| 152 | } |
| 153 | |
| 154 | //////////////////////////////////////////////////////////////////////// |
| 155 | // |
| 156 | // Description: |
| 157 | // Override pop to free up plane structures that were created in |
| 158 | // the instance that was removed from the state. |
| 159 | // |
| 160 | // Use: public |
| 161 | |
| 162 | void |
| 163 | SoClipPlaneElement::pop(SoState *, const SoElement *prevTopElement) |
| 164 | // |
| 165 | //////////////////////////////////////////////////////////////////////// |
| 166 | { |
| 167 | const SoClipPlaneElement *prevElt = |
| 168 | (const SoClipPlaneElement *) prevTopElement; |
| 169 | |
| 170 | // Free up any plane structures that were created by prevElt |
| 171 | for (int i = prevElt->startIndex; i < prevElt->planes.getLength(); i++) |
| 172 | delete (So_ClipPlane *) prevElt->planes[i]; |
| 173 | } |
| 174 | |
| 175 | //////////////////////////////////////////////////////////////////////// |
| 176 | // |
| 177 | // Description: |
| 178 | // Returns the first (top) instance of the element in the state. |
| 179 | // |
| 180 | // Use: public, static |
| 181 | |
| 182 | const SoClipPlaneElement * |
| 183 | SoClipPlaneElement::getInstance(SoState *state) |
| 184 | // |
| 185 | //////////////////////////////////////////////////////////////////////// |
| 186 | { |
| 187 | return (const SoClipPlaneElement *) |
| 188 | getConstElement(state, classStackIndex); |
| 189 | } |
| 190 | |
| 191 | //////////////////////////////////////////////////////////////////////// |
| 192 | // |
| 193 | // Description: |
| 194 | // Returns the number of planes in the element |
| 195 | // |
| 196 | // Use: public |
| 197 | |
| 198 | int |
| 199 | SoClipPlaneElement::getNum() const |
| 200 | // |
| 201 | //////////////////////////////////////////////////////////////////////// |
| 202 | { |
| 203 | return planes.getLength(); |
| 204 | } |
| 205 | |
| 206 | //////////////////////////////////////////////////////////////////////// |
| 207 | // |
| 208 | // Description: |
| 209 | // Returns the nth plane stored in an instance in object or world |
| 210 | // space. |
| 211 | // |
| 212 | // Use: public |
| 213 | |
| 214 | const SbPlane & |
| 215 | SoClipPlaneElement::get(int index, SbBool inWorldSpace) const |
| 216 | // |
| 217 | //////////////////////////////////////////////////////////////////////// |
| 218 | { |
| 219 | #ifdef DEBUG |
| 220 | if (index < 0 || index >= planes.getLength()) |
| 221 | SoDebugError::post("SoClipPlaneElement::get", |
| 222 | "Index (%d) is out of range 0 - %d", |
| 223 | index, planes.getLength() - 1); |
| 224 | #endif /* DEBUG */ |
| 225 | |
| 226 | So_ClipPlane *plane = (So_ClipPlane *) planes[index]; |
| 227 | |
| 228 | if (! inWorldSpace) |
| 229 | return plane->objPlane; |
| 230 | |
| 231 | // Transform plane into world space if necessary |
| 232 | if (! plane->worldPlaneValid) { |
| 233 | plane->worldPlane = plane->objPlane; |
| 234 | plane->worldPlane.transform(plane->objToWorld); |
| 235 | } |
| 236 | return plane->worldPlane; |
| 237 | } |
| 238 | |
| 239 | //////////////////////////////////////////////////////////////////////// |
| 240 | // |
| 241 | // Description: |
| 242 | // Prints element for debugging. |
| 243 | // |
| 244 | // Use: public |
| 245 | // |
| 246 | //////////////////////////////////////////////////////////////////////// |
| 247 | |
| 248 | #ifdef DEBUG |
| 249 | void |
| 250 | SoClipPlaneElement::print(FILE *fp) const |
| 251 | { |
| 252 | SoAccumulatedElement::print(fp); |
| 253 | } |
| 254 | #else /* DEBUG */ |
| 255 | void |
| 256 | SoClipPlaneElement::print(FILE *) const |
| 257 | { |
| 258 | } |
| 259 | #endif /* DEBUG */ |
| 260 | |
| 261 | //////////////////////////////////////////////////////////////////////// |
| 262 | // |
| 263 | // Description: |
| 264 | // Adds clipping plane to element. |
| 265 | // |
| 266 | // Use: protected, virtual |
| 267 | |
| 268 | void |
| 269 | SoClipPlaneElement::addToElt(const SbPlane &plane, |
| 270 | const SbMatrix &modelMatrix) |
| 271 | // |
| 272 | //////////////////////////////////////////////////////////////////////// |
| 273 | { |
| 274 | So_ClipPlane *newPlane = new So_ClipPlane; |
| 275 | |
| 276 | newPlane->objPlane = plane; |
| 277 | newPlane->objToWorld = modelMatrix; |
| 278 | newPlane->worldPlaneValid = FALSE; |
| 279 | |
| 280 | planes.append(newPlane); |
| 281 | } |