00001 /* 00002 * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc. 00003 * All rights reserved. This program is made available under the terms of the 00004 * Eclipse Public License v1.0 which accompanies this distribution, and is 00005 * available at http://www.eclipse.org/legal/epl-v10.html 00006 * Contributors: 00007 * National Institute of Advanced Industrial Science and Technology (AIST) 00008 */ 00009 00014 #include "VrmlNodes.h" 00015 00016 using namespace hrp; 00017 00018 00019 const char* VrmlNode::getLabelOfFieldType(int type) 00020 { 00021 switch(type){ 00022 case SFINT32: return "SFInt32"; 00023 case MFINT32: return "MFInt32"; 00024 case SFFLOAT: return "SFFloat"; 00025 case MFFLOAT: return "MFFloat"; 00026 case SFVEC2F: return "SFVec3f"; 00027 case MFVEC2F: return "MFVec2f"; 00028 case SFVEC3F: return "SFVec3f"; 00029 case MFVEC3F: return "MFVec3f"; 00030 case SFROTATION: return "SFRotation"; 00031 case MFROTATION: return "MFRotation"; 00032 case SFTIME: return "SFTime"; 00033 case MFTIME: return "MFTime"; 00034 case SFCOLOR: return "SFColor"; 00035 case MFCOLOR: return "MFColor"; 00036 case SFSTRING: return "SFString"; 00037 case MFSTRING: return "MFString"; 00038 case SFNODE: return "SFNode"; 00039 case MFNODE: return "MFNode"; 00040 case SFBOOL: return "SFBool"; 00041 case SFIMAGE: return "SFImage"; 00042 default: return "Unknown Field Type"; 00043 break; 00044 } 00045 } 00046 00047 00048 VrmlNode::VrmlNode() 00049 { 00050 refCounter = 0; 00051 } 00052 00053 00054 VrmlNode::~VrmlNode() 00055 { 00056 00057 } 00058 00059 00060 bool VrmlNode::isCategoryOf(VrmlNodeCategory category) 00061 { 00062 return (category == ANY_NODE) ? true : categorySet.test(category); 00063 } 00064 00065 00066 00067 VrmlUnsupportedNode::VrmlUnsupportedNode(const std::string& nodeTypeName) : 00068 nodeTypeName(nodeTypeName) 00069 { 00070 categorySet.set(TOP_NODE); 00071 categorySet.set(CHILD_NODE); 00072 } 00073 00074 00075 VrmlViewpoint::VrmlViewpoint() 00076 { 00077 categorySet.set(TOP_NODE); 00078 categorySet.set(BINDABLE_NODE); 00079 categorySet.set(CHILD_NODE); 00080 00081 fieldOfView = 0.785398; 00082 jump = true; 00083 00084 orientation[0] = orientation[1] = orientation[3] = 0.0; 00085 orientation[2] = 1.0; 00086 00087 position[0] = position[1] = 0.0; 00088 position[2] = 10; 00089 } 00090 00091 00092 VrmlNavigationInfo::VrmlNavigationInfo() : 00093 avatarSize(3) 00094 { 00095 categorySet.set(TOP_NODE); 00096 categorySet.set(BINDABLE_NODE); 00097 categorySet.set(CHILD_NODE); 00098 00099 avatarSize[0] = 0.25; 00100 avatarSize[1] = 1.6; 00101 avatarSize[2] = 0.75; 00102 00103 headlight = true; 00104 speed = 1.0; 00105 visibilityLimit = 0.0; 00106 00107 type.push_back("WALK"); 00108 } 00109 00110 00111 VrmlBackground::VrmlBackground() 00112 { 00113 categorySet.set(TOP_NODE); 00114 categorySet.set(BINDABLE_NODE); 00115 categorySet.set(CHILD_NODE); 00116 } 00117 00118 00119 AbstractVrmlGroup::AbstractVrmlGroup() 00120 { 00121 categorySet.set(TOP_NODE); 00122 categorySet.set(GROUPING_NODE); 00123 categorySet.set(CHILD_NODE); 00124 } 00125 00126 00127 void AbstractVrmlGroup::removeChild(int childIndex) 00128 { 00129 replaceChild(childIndex, 0); 00130 } 00131 00132 00133 VrmlGroup::VrmlGroup() 00134 { 00135 bboxCenter[0] = bboxCenter[1] = bboxCenter[2] = 0.0; 00136 bboxSize[0] = bboxSize[1] = bboxSize[2] = -1; 00137 } 00138 00139 MFNode& VrmlGroup::getChildren() 00140 { 00141 return children; 00142 } 00143 00144 int VrmlGroup::countChildren() 00145 { 00146 return children.size(); 00147 } 00148 00149 00150 VrmlNode* VrmlGroup::getChild(int index) 00151 { 00152 return children[index].get(); 00153 } 00154 00155 00156 void VrmlGroup::replaceChild(int childIndex, VrmlNode* childNode) 00157 { 00158 if(!childNode){ 00159 children.erase(children.begin() + childIndex); 00160 } else { 00161 children[childIndex] = childNode; 00162 } 00163 } 00164 00165 00166 VrmlTransform::VrmlTransform() 00167 { 00168 center[0] = center[1] = center[2] = 0.0; 00169 rotation[0] = rotation[1] = rotation[3] = 0.0; 00170 rotation[2] = 1.0; 00171 scale[0] = scale[1] = scale[2] = 1.0; 00172 scaleOrientation[0] = scaleOrientation[1] = scaleOrientation[3] = 0.0; 00173 scaleOrientation[2] = 1.0; 00174 translation[0] = translation[1] = translation[2] = 0.0; 00175 } 00176 00177 VrmlInline::VrmlInline() 00178 { 00179 categorySet.set(INLINE_NODE); 00180 } 00181 00182 VrmlShape::VrmlShape() 00183 { 00184 categorySet.set(TOP_NODE); 00185 categorySet.set(CHILD_NODE); 00186 categorySet.set(SHAPE_NODE); 00187 } 00188 00189 00190 VrmlAppearance::VrmlAppearance() 00191 { 00192 categorySet.set(APPEARANCE_NODE); 00193 } 00194 00195 00196 VrmlMaterial::VrmlMaterial() 00197 { 00198 categorySet.set(MATERIAL_NODE); 00199 00200 diffuseColor [0] = diffuseColor [1] = diffuseColor [2] = 0.8; 00201 emissiveColor[0] = emissiveColor[1] = emissiveColor[2] = 0.0; 00202 specularColor[0] = specularColor[1] = specularColor[2] = 0.0; 00203 ambientIntensity = 0.2; 00204 shininess = 0.2; 00205 transparency = 0.0; 00206 } 00207 00208 00209 VrmlTexture::VrmlTexture() 00210 { 00211 categorySet.set(TEXTURE_NODE); 00212 } 00213 00214 00215 VrmlImageTexture::VrmlImageTexture() 00216 { 00217 repeatS = true; 00218 repeatT = true; 00219 } 00220 00221 00222 VrmlTextureTransform::VrmlTextureTransform() 00223 { 00224 categorySet.set(TEXTURE_TRANSFORM_NODE); 00225 00226 center[0] = center[1] = 0.0; 00227 scale[0] = scale[1] = 1.0; 00228 translation[0] = translation[1] = 0.0; 00229 rotation = 0.0; 00230 } 00231 00232 00233 VrmlGeometry::VrmlGeometry() 00234 { 00235 categorySet.set(GEOMETRY_NODE); 00236 } 00237 00238 00239 VrmlBox::VrmlBox() 00240 { 00241 size[0] = size[1] = size[2] = 2.0; 00242 } 00243 00244 00245 VrmlCone::VrmlCone() 00246 { 00247 bottom = true; 00248 bottomRadius = 1.0; 00249 height = 2.0; 00250 side = true; 00251 } 00252 00253 00254 VrmlCylinder::VrmlCylinder() 00255 { 00256 height = 2.0; 00257 radius = 1.0; 00258 bottom = true; 00259 side = true; 00260 top = true; 00261 } 00262 00263 00264 VrmlSphere::VrmlSphere() 00265 { 00266 radius = 1.0; 00267 } 00268 00269 00270 VrmlFontStyle::VrmlFontStyle() 00271 { 00272 categorySet.set(FONT_STYLE_NODE); 00273 00274 family.push_back("SERIF"); 00275 horizontal = true; 00276 justify.push_back("BEGIN"); 00277 leftToRight = true; 00278 size = 1.0; 00279 spacing = 1.0; 00280 style = "PLAIN"; 00281 topToBottom = true; 00282 } 00283 00284 00285 VrmlText::VrmlText() 00286 { 00287 maxExtent = 0.0; 00288 } 00289 00290 00291 VrmlIndexedLineSet::VrmlIndexedLineSet() 00292 { 00293 colorPerVertex = true; 00294 } 00295 00296 00297 VrmlIndexedFaceSet::VrmlIndexedFaceSet() 00298 { 00299 ccw = true; 00300 convex = true; 00301 creaseAngle = 0.0; 00302 normalPerVertex = true; 00303 solid = true; 00304 } 00305 00306 00307 VrmlColor::VrmlColor() 00308 { 00309 categorySet.set(COLOR_NODE); 00310 } 00311 00312 00313 VrmlCoordinate::VrmlCoordinate() 00314 { 00315 categorySet.set(COORDINATE_NODE); 00316 } 00317 00318 00319 VrmlTextureCoordinate::VrmlTextureCoordinate() 00320 { 00321 categorySet.set(TEXTURE_COORDINATE_NODE); 00322 } 00323 00324 00325 VrmlNormal::VrmlNormal() 00326 { 00327 categorySet.set(NORMAL_NODE); 00328 } 00329 00330 00331 VrmlCylinderSensor::VrmlCylinderSensor() 00332 { 00333 categorySet.set(CHILD_NODE); 00334 categorySet.set(SENSOR_NODE); 00335 00336 autoOffset = true; 00337 diskAngle = 0.262; 00338 enabled = true; 00339 maxAngle = -1; 00340 minAngle = 0; 00341 offset = 0; 00342 } 00343 00344 00345 00346 00347 // ##### VrmlPointSet() 00348 VrmlPointSet::VrmlPointSet() 00349 { 00350 coord = NULL; 00351 color = NULL; 00352 } 00353 00354 00355 00356 // ##### VrmlPixelTexture() 00357 VrmlPixelTexture::VrmlPixelTexture() 00358 { 00359 image.width = 0; 00360 image.height = 0; 00361 image.numComponents = 0; 00362 image.pixels.clear(); 00363 00364 repeatS = true; 00365 repeatT = true; 00366 } 00367 00368 00369 00370 // ##### VrmlMovieTexture() 00371 VrmlMovieTexture::VrmlMovieTexture() 00372 { 00373 // url 00374 loop = false; 00375 speed = 0; 00376 startTime = 0; 00377 stopTime = 0; 00378 repeatS = true; 00379 repeatT = true; 00380 } 00381 00382 00383 00384 // ##### VrmlElevationGrid() 00385 VrmlElevationGrid::VrmlElevationGrid() 00386 { 00387 xDimension = 0; 00388 zDimension = 0; 00389 xSpacing = 0.0; 00390 zSpacing = 0.0; 00391 // height // MFFloat 00392 ccw = true; 00393 colorPerVertex = true; 00394 creaseAngle = 0.0; 00395 normalPerVertex = true; 00396 solid = true; 00397 color = NULL; 00398 normal = NULL; 00399 texCoord = NULL; 00400 } 00401 00402 00403 00404 // ##### VrmlExtrusion() 00405 VrmlExtrusion::VrmlExtrusion() 00406 { 00407 // crossSection 00408 // spine 00409 beginCap = true; 00410 endCap = true; 00411 solid = true; 00412 ccw = true; 00413 convex = true; 00414 creaseAngle = 0; 00415 SFRotation ori= {0,0,1,0}; 00416 orientation.push_back(ori); 00417 SFVec2f sc ={ 1,1 }; 00418 scale.push_back(sc); 00419 00420 } 00421 00422 00423 VrmlSwitch::VrmlSwitch() 00424 { 00425 whichChoice = -1; 00426 } 00427 00428 MFNode& VrmlSwitch::getChildren() 00429 { 00430 return choice; 00431 } 00432 00433 int VrmlSwitch::countChildren() 00434 { 00435 return choice.size(); 00436 } 00437 00438 00439 VrmlNode* VrmlSwitch::getChild(int index) 00440 { 00441 return choice[index].get(); 00442 } 00443 00444 00445 void VrmlSwitch::replaceChild(int childIndex, VrmlNode* childNode) 00446 { 00447 if(!childNode){ 00448 choice.erase(choice.begin() + childIndex); 00449 if(whichChoice == childIndex){ 00450 whichChoice = -1; 00451 } else if(whichChoice > childIndex){ 00452 whichChoice -= 1; 00453 } 00454 } else { 00455 choice[childIndex] = childNode; 00456 } 00457 } 00458 00459 00460 VrmlLOD::VrmlLOD() 00461 { 00462 center[0] = center[1] = center[2] = 0.0; 00463 } 00464 00465 MFNode& VrmlLOD::getChildren() 00466 { 00467 return level; 00468 } 00469 00470 int VrmlLOD::countChildren() 00471 { 00472 return level.size(); 00473 } 00474 00475 00476 VrmlNode* VrmlLOD::getChild(int index) 00477 { 00478 return level[index].get(); 00479 } 00480 00481 00482 void VrmlLOD::replaceChild(int childIndex, VrmlNode* childNode) 00483 { 00484 if(!childNode){ 00485 level.erase(level.begin() + childIndex); 00486 if(!level.empty()){ 00487 int rangeIndexToRemove = (childIndex > 0) ? (childIndex - 1) : 0; 00488 range.erase(range.begin() + rangeIndexToRemove); 00489 } 00490 } else { 00491 level[childIndex] = childNode; 00492 } 00493 } 00494 00495 00496 VrmlCollision::VrmlCollision() 00497 { 00498 collide = true; 00499 } 00500 00501 00502 VrmlAnchor::VrmlAnchor() 00503 { 00504 00505 } 00506 00507 00508 VrmlBillboard::VrmlBillboard() 00509 { 00510 axisOfRotation[0] = axisOfRotation[1] = axisOfRotation[3] = 0; 00511 } 00512 00513 00514 VrmlFog::VrmlFog() 00515 { 00516 color[0] = color[1] = color[3] = 0.0f; 00517 visibilityRange = 0.0f; 00518 fogType = "LINEAR"; 00519 } 00520 00521 00522 VrmlWorldInfo::VrmlWorldInfo() 00523 { 00524 categorySet.set(TOP_NODE); 00525 } 00526 00527 00528 VrmlPointLight::VrmlPointLight() 00529 { 00530 categorySet.set(TOP_NODE); 00531 categorySet.set(CHILD_NODE); 00532 categorySet.set(LIGHT_NODE); 00533 00534 location[0] = location[1] = location[2] = 0.0f; 00535 on = true; 00536 intensity = 1.0f; 00537 color[0] = color[1] = color[2] = 1.0f; 00538 radius = 100.0f; 00539 ambientIntensity = 0.0f; 00540 attenuation[0] = 1; 00541 attenuation[1] = attenuation[2] = 0; 00542 } 00543 00544 00545 VrmlDirectionalLight::VrmlDirectionalLight() 00546 { 00547 categorySet.set(TOP_NODE); 00548 categorySet.set(CHILD_NODE); 00549 categorySet.set(LIGHT_NODE); 00550 00551 ambientIntensity = 0.0f; 00552 color[0] = color[1] = color[2] = 1.0f; 00553 direction[0] = direction[1] = 0.0f; 00554 direction[2] = -1.0f; 00555 intensity = 1.0f; 00556 on = true; 00557 } 00558 00559 00560 VrmlSpotLight::VrmlSpotLight() 00561 { 00562 categorySet.set(TOP_NODE); 00563 categorySet.set(CHILD_NODE); 00564 categorySet.set(LIGHT_NODE); 00565 00566 location[0] = location[1] = location[2] = 0.0f; 00567 direction[0] = direction[1] = 0.0f; 00568 direction[2] = -1.0f; 00569 on = true; 00570 color[0] = color[1] = color[2] = 1.0f; 00571 intensity = 1.0f; 00572 radius = 100.0f; 00573 ambientIntensity = 0.0f; 00574 attenuation[0] = 1.0f; 00575 attenuation[1] = attenuation[2] = 0.0f; 00576 beamWidth = 1.570796f; 00577 cutOffAngle = 0.785398f; 00578 } 00579 00580 00581 00582 VrmlVariantField::VrmlVariantField(VrmlFieldTypeId initialTypeId) 00583 { 00584 typeId_ = UNDETERMINED_FIELD_TYPE; 00585 valueObj = 0; 00586 setType(initialTypeId); 00587 } 00588 00589 VrmlVariantField::VrmlVariantField() 00590 { 00591 typeId_ = UNDETERMINED_FIELD_TYPE; 00592 valueObj = 0; 00593 } 00594 00595 VrmlVariantField::VrmlVariantField(const VrmlVariantField& org) 00596 { 00597 valueObj = 0; 00598 copy(org); 00599 } 00600 00601 VrmlVariantField& VrmlVariantField::operator=(const VrmlVariantField& org) 00602 { 00603 deleteObj(); 00604 copy(org); 00605 return *this; 00606 } 00607 00608 void VrmlVariantField::copy(const VrmlVariantField& org) 00609 { 00610 typeId_ = org.typeId_; 00611 00612 if(!org.valueObj){ 00613 v = org.v; 00614 } else { 00615 switch(typeId_){ 00616 case MFINT32: valueObj = new MFInt32(*((MFInt32*)org.valueObj)); break; 00617 case MFFLOAT: valueObj = new MFFloat(*((MFFloat*)org.valueObj)); break; 00618 case MFVEC2F: valueObj = new MFVec2f(*((MFVec2f*)org.valueObj)); break; 00619 case MFVEC3F: valueObj = new MFVec3f(*((MFVec3f*)org.valueObj)); break; 00620 case MFROTATION: valueObj = new MFRotation(*((MFRotation*)org.valueObj)); break; 00621 case MFTIME: valueObj = new MFTime(*((MFTime*)org.valueObj)); break; 00622 case MFCOLOR: valueObj = new MFColor(*((MFColor*)org.valueObj)); break; 00623 case SFSTRING: valueObj = new SFString(*((SFString*)org.valueObj)); break; 00624 case MFSTRING: valueObj = new MFString(*((MFString*)org.valueObj)); break; 00625 case SFNODE: valueObj = new SFNode(*((SFNode*)org.valueObj)); break; 00626 case MFNODE: valueObj = new MFNode(*((MFNode*)org.valueObj)); break; 00627 case SFIMAGE: valueObj = new SFImage(*((SFImage*)org.valueObj)); break; 00628 default: 00629 break; 00630 } 00631 } 00632 } 00633 00634 00636 void VrmlVariantField::setType(VrmlFieldTypeId typeId0) 00637 { 00638 if(typeId_ == UNDETERMINED_FIELD_TYPE){ 00639 typeId_ = typeId0; 00640 00641 switch(typeId_){ 00642 case MFINT32: valueObj = new MFInt32; break; 00643 case MFFLOAT: valueObj = new MFFloat; break; 00644 case MFVEC2F: valueObj = new MFVec2f; break; 00645 case MFVEC3F: valueObj = new MFVec3f; break; 00646 case MFROTATION: valueObj = new MFRotation; break; 00647 case MFTIME: valueObj = new MFTime; break; 00648 case MFCOLOR: valueObj = new MFColor; break; 00649 case SFSTRING: valueObj = new SFString; break; 00650 case MFSTRING: valueObj = new MFString; break; 00651 case SFNODE: valueObj = new SFNode; break; 00652 case MFNODE: valueObj = new MFNode; break; 00653 case SFIMAGE: valueObj = new SFImage; break; 00654 default: 00655 break; 00656 } 00657 } 00658 } 00659 00660 00661 void VrmlVariantField::deleteObj() 00662 { 00663 if(valueObj){ 00664 switch(typeId_){ 00665 case MFINT32: delete (MFInt32*)valueObj; break; 00666 case MFFLOAT: delete (MFFloat*)valueObj; break; 00667 case MFVEC2F: delete (MFVec2f*)valueObj; break; 00668 case MFVEC3F: delete (MFVec3f*)valueObj; break; 00669 case MFROTATION: delete (MFRotation*)valueObj; break; 00670 case MFTIME: delete (MFTime*)valueObj; break; 00671 case MFCOLOR: delete (MFColor*)valueObj; break; 00672 case SFSTRING: delete (SFString*)valueObj; break; 00673 case MFSTRING: delete (MFString*)valueObj; break; 00674 case SFNODE: delete (SFNode*)valueObj; break; 00675 case MFNODE: delete (MFNode*)valueObj; break; 00676 case SFIMAGE: delete (SFImage*)valueObj; break; 00677 default: 00678 break; 00679 } 00680 valueObj = 0; 00681 } 00682 } 00683 00684 00685 VrmlVariantField::~VrmlVariantField() 00686 { 00687 deleteObj(); 00688 } 00689 00690 00691 VrmlProto::VrmlProto(const std::string& n) : protoName(n) 00692 { 00693 categorySet.set(TOP_NODE); 00694 categorySet.set(PROTO_DEF_NODE); 00695 } 00696 00697 00698 VrmlProtoInstance::VrmlProtoInstance(VrmlProtoPtr proto0) : 00699 proto(proto0), 00700 fields(proto0->fields) 00701 { 00702 categorySet.set(TOP_NODE); 00703 categorySet.set(PROTO_INSTANCE_NODE);; 00704 categorySet.set(CHILD_NODE); 00705 }