SoAxisCrossKit.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <Inventor/actions/SoGetBoundingBoxAction.h>
00028 # include <Inventor/actions/SoGLRenderAction.h>
00029 # include <Inventor/nodes/SoBaseColor.h>
00030 # include <Inventor/nodes/SoShape.h>
00031 # include <Inventor/nodes/SoScale.h>
00032 # include <Inventor/nodes/SoCone.h>
00033 # include <Inventor/nodes/SoCoordinate3.h>
00034 # include <Inventor/nodes/SoCube.h>
00035 # include <Inventor/nodes/SoLineSet.h>
00036 # include <Inventor/nodes/SoSeparator.h>
00037 # include <Inventor/nodekits/SoShapeKit.h>
00038 # include <Inventor/elements/SoViewportRegionElement.h>
00039 # include <Inventor/elements/SoViewVolumeElement.h>
00040 # include <Inventor/elements/SoModelMatrixElement.h>
00041 #endif
00042 
00043 
00044 #include "SoAxisCrossKit.h"
00045 
00046 using namespace Gui;
00047 
00048 
00049 SO_KIT_SOURCE(SoShapeScale);
00050 
00051 //  Constructor.
00052 SoShapeScale::SoShapeScale(void)
00053 {
00054     SO_KIT_CONSTRUCTOR(SoShapeScale);
00055 
00056     SO_KIT_ADD_FIELD(active, (TRUE));
00057     SO_KIT_ADD_FIELD(scaleFactor, (1.0f));
00058 
00059     SO_KIT_ADD_CATALOG_ENTRY(topSeparator, SoSeparator, FALSE, this, "", FALSE);
00060     SO_KIT_ADD_CATALOG_ABSTRACT_ENTRY(shape, SoNode, SoCube, TRUE, topSeparator, "", TRUE);
00061     SO_KIT_ADD_CATALOG_ENTRY(scale, SoScale, FALSE, topSeparator, shape, FALSE);
00062 
00063     SO_KIT_INIT_INSTANCE();
00064 }
00065 
00066 // Destructor.
00067 SoShapeScale::~SoShapeScale()
00068 {
00069 }
00070 
00071 void
00072 SoShapeScale::initClass(void)
00073 {
00074     SO_KIT_INIT_CLASS(SoShapeScale, SoBaseKit, "BaseKit");
00075 }
00076 
00077 void
00078 SoShapeScale::GLRender(SoGLRenderAction * action)
00079 {
00080     SoState * state = action->getState();
00081 
00082     SoScale * scale = static_cast<SoScale*>(this->getAnyPart(SbName("scale"), TRUE));
00083     if (!this->active.getValue()) {
00084         SbVec3f v(1.0f, 1.0f, 1.0f);
00085         if (scale->scaleFactor.getValue() != v)
00086             scale->scaleFactor = v;
00087     }
00088     else {
00089         const SbViewportRegion & vp = SoViewportRegionElement::get(state);
00090         const SbViewVolume & vv = SoViewVolumeElement::get(state);
00091         SbVec3f center(0.0f, 0.0f, 0.0f);
00092         float nsize = this->scaleFactor.getValue() / float(vp.getViewportSizePixels()[1]);
00093         SoModelMatrixElement::get(state).multVecMatrix(center, center); // world coords
00094         float sf = vv.getWorldToScreenScale(center, nsize);
00095         SbVec3f v(sf, sf, sf);
00096         if (scale->scaleFactor.getValue() != v)
00097             scale->scaleFactor = v;
00098     }
00099 
00100     inherited::GLRender(action);
00101 }
00102 
00103 // --------------------------------------------------------------
00104 
00105 SO_KIT_SOURCE(SoAxisCrossKit);
00106 
00107 void
00108 SoAxisCrossKit::initClass()
00109 {
00110    SO_KIT_INIT_CLASS(SoAxisCrossKit,SoBaseKit, "BaseKit");
00111 }
00112 
00113 SoAxisCrossKit::SoAxisCrossKit()
00114 {
00115    SO_KIT_CONSTRUCTOR(SoAxisCrossKit);
00116 
00117    // Add the parts to the catalog...
00118    SO_KIT_ADD_CATALOG_ENTRY(xAxis, SoShapeKit, 
00119                             TRUE, this,"", TRUE);
00120    SO_KIT_ADD_CATALOG_ENTRY(xHead, SoShapeKit, 
00121                             TRUE, this,"", TRUE);
00122    SO_KIT_ADD_CATALOG_ENTRY(yAxis, SoShapeKit, 
00123                             TRUE, this,"", TRUE);
00124    SO_KIT_ADD_CATALOG_ENTRY(yHead, SoShapeKit, 
00125                             TRUE, this,"", TRUE);
00126    SO_KIT_ADD_CATALOG_ENTRY(zAxis, SoShapeKit, 
00127                             TRUE, this,"", TRUE);
00128    SO_KIT_ADD_CATALOG_ENTRY(zHead, SoShapeKit, 
00129                             TRUE, this,"", TRUE);
00130 
00131    SO_KIT_INIT_INSTANCE();
00132 
00133    createAxes();
00134 }
00135 
00136 SoAxisCrossKit::~SoAxisCrossKit()
00137 {
00138 }
00139 
00140 // This kit is made up entirely of SoShapeKits.
00141 // Since SoShapeKits do not affect state, neither does this.
00142 SbBool
00143 SoAxisCrossKit::affectsState() const
00144 {
00145    return FALSE;
00146 }
00147 
00148 void SoAxisCrossKit::addWriteReference(SoOutput * out, SbBool isfromfield)
00149 {
00150     // this node should not be written out to a file
00151 }
00152 
00153 void SoAxisCrossKit::getBoundingBox(SoGetBoundingBoxAction * action)
00154 {
00155     inherited::getBoundingBox(action);
00156     action->resetCenter();
00157     action->setCenter(SbVec3f(0,0,0), FALSE);
00158 }
00159 
00160 // Set up parts for default configuration of the jumping jack
00161 void
00162 SoAxisCrossKit::createAxes()
00163 {
00164    // Create the heads.
00165    SoCone *head = new SoCone;
00166    head->bottomRadius.setValue(5);
00167    head->height.setValue(10);
00168    setPart("xHead.shape", head);
00169    setPart("yHead.shape", head);
00170    setPart("zHead.shape", head);
00171 
00172    // Create the axes.
00173    SoCoordinate3* coords = new SoCoordinate3;
00174    coords->point.set1Value(0, SbVec3f(0,0,0));
00175    coords->point.set1Value(1, SbVec3f(90,0,0));
00176    setPart("xAxis.coordinate3", coords);
00177    setPart("yAxis.coordinate3", coords);
00178    setPart("zAxis.coordinate3", coords);
00179 
00180    SoLineSet *shape = new SoLineSet;
00181    setPart("xAxis.shape", shape);
00182    setPart("yAxis.shape", shape);
00183    setPart("zAxis.shape", shape);
00184 
00185    // Place the axes ând heads
00186    set("yAxis.transform", "rotation 0 0 1 1.5707999");
00187    set("zAxis.transform", "rotation 0 1 0 -1.5707999");
00188 
00189    set("xHead.transform", "translation 95 0 0");
00190    set("xHead.transform", "scaleFactor 0.5 1.5 0.5");
00191    set("xHead.transform", "rotation 0 0 -1  1.5707999");
00192 
00193    set("yHead.transform", "translation 0 95 0");
00194    set("yHead.transform", "scaleFactor 0.5 1.5 0.5");
00195    set("yHead.transform", "rotation 0 0 1 0");
00196 
00197    set("zHead.transform", "translation 0 0 95");
00198    set("zHead.transform", "scaleFactor 0.5 1.5 0.5");
00199    set("zHead.transform", "rotation 1 0 0  1.5707999");
00200 
00201    // Set colors & styles
00202    set("xAxis.appearance.drawStyle", "lineWidth 1");
00203    set("yAxis.appearance.drawStyle", "lineWidth 1");
00204    set("zAxis.appearance.drawStyle", "lineWidth 1");
00205    set("xAxis.appearance.material", "diffuseColor 0.5 0.125 0.125");
00206    set("xHead.appearance.material", "diffuseColor 0.5 0.125 0.125");
00207    set("yAxis.appearance.material", "diffuseColor 0.125 0.5 0.125");
00208    set("yHead.appearance.material", "diffuseColor 0.125 0.5 0.125");
00209    set("zAxis.appearance.material", "diffuseColor 0.125 0.125 0.5");
00210    set("zHead.appearance.material", "diffuseColor 0.125 0.125 0.5");
00211 
00212    // Make unpickable
00213    set("xAxis.pickStyle", "style UNPICKABLE");
00214    set("xHead.pickStyle", "style UNPICKABLE");
00215    set("yAxis.pickStyle", "style UNPICKABLE");
00216    set("yHead.pickStyle", "style UNPICKABLE");
00217    set("zAxis.pickStyle", "style UNPICKABLE");
00218    set("zHead.pickStyle", "style UNPICKABLE");
00219 }

Generated on Wed Nov 23 19:00:38 2011 for FreeCAD by  doxygen 1.6.1