|
| 1 | +/* |
| 2 | +For more information, please see: http://software.sci.utah.edu |
| 3 | +
|
| 4 | +The MIT License |
| 5 | +
|
| 6 | +Copyright (c) 2015 Scientific Computing and Imaging Institute, |
| 7 | +University of Utah. |
| 8 | +
|
| 9 | +License for the specific language governing rights and limitations under |
| 10 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 11 | +copy of this software and associated documentation files (the "Software"), |
| 12 | +to deal in the Software without restriction, including without limitation |
| 13 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 | +and/or sell copies of the Software, and to permit persons to whom the |
| 15 | +Software is furnished to do so, subject to the following conditions: |
| 16 | +
|
| 17 | +The above copyright notice and this permission notice shall be included |
| 18 | +in all copies or substantial portions of the Software. |
| 19 | +
|
| 20 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 21 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 26 | +DEALINGS IN THE SOFTWARE. |
| 27 | +*/ |
| 28 | + |
| 29 | +#include <Core/Datatypes/Color.h> |
| 30 | +#include <Graphics/Glyphs/GlyphGeom.h> |
| 31 | +#include <Graphics/Widgets/ArrowWidget.h> |
| 32 | +#include <Graphics/Widgets/WidgetFactory.h> |
| 33 | + |
| 34 | +using namespace SCIRun; |
| 35 | +using namespace SCIRun::Core; |
| 36 | +using namespace SCIRun::Core::Datatypes; |
| 37 | +using namespace SCIRun::Graphics::Datatypes; |
| 38 | +using namespace SCIRun::Core::Geometry; |
| 39 | + |
| 40 | +ArrowWidget::ArrowWidget(const GeometryIDGenerator &idGenerator, |
| 41 | + const std::string &name, double scale, |
| 42 | + const Point &pos, const Vector &dir, int resolution, |
| 43 | + bool show_as_vector, size_t widget_num, size_t widget_iter, |
| 44 | + const BBox &bbox) |
| 45 | + : CompositeWidget(idGenerator, name) { |
| 46 | + static const ColorRGB deflPointCol_ = ColorRGB(0.54, 0.6, 1.0); |
| 47 | + static const ColorRGB deflCol_ = ColorRGB(0.5, 0.5, 0.5); |
| 48 | + static const ColorRGB resizeCol_ = ColorRGB(0.54, 1.0, 0.60); |
| 49 | + ColorRGB sphereCol = (show_as_vector) ? deflPointCol_ : resizeCol_; |
| 50 | + |
| 51 | + static const double sphereRadius_ = 0.25; |
| 52 | + static const double cylinderRadius_ = 0.12; |
| 53 | + static const double coneRadius_ = 0.25; |
| 54 | + static const double diskRadius_ = 0.25; |
| 55 | + static const double diskDistFromCenter_ = 0.85; |
| 56 | + static const double diskWidth_ = 0.05; |
| 57 | + |
| 58 | + if (resolution < 3) resolution = 10; |
| 59 | + |
| 60 | + isVector_ = show_as_vector; |
| 61 | + auto colorScheme = ColorScheme::COLOR_UNIFORM; |
| 62 | + std::stringstream ss; |
| 63 | + ss << pos << dir << static_cast<int>(colorScheme); |
| 64 | + |
| 65 | + auto uniqueNodeID = uniqueID() + "widget" + ss.str(); |
| 66 | + |
| 67 | + // Graphics::GlyphGeom glyphs; |
| 68 | + ColorRGB node_color; |
| 69 | + |
| 70 | + // auto renState = getWidgetRenderState(defaultColor); |
| 71 | + |
| 72 | + Point bmin = pos; |
| 73 | + Point bmax = pos + dir * scale; |
| 74 | + |
| 75 | + // Fix degenerate boxes. |
| 76 | + const double size_estimate = std::max((bmax - bmin).length() * 0.01, 1.0e-5); |
| 77 | + if (std::abs(bmax.x() - bmin.x()) < 1.0e-6) |
| 78 | + { |
| 79 | + bmin.x(bmin.x() - size_estimate); |
| 80 | + bmax.x(bmax.x() + size_estimate); |
| 81 | + } |
| 82 | + if (std::abs(bmax.y() - bmin.y()) < 1.0e-6) |
| 83 | + { |
| 84 | + bmin.y(bmin.y() - size_estimate); |
| 85 | + bmax.y(bmax.y() + size_estimate); |
| 86 | + } |
| 87 | + if (std::abs(bmax.z() - bmin.z()) < 1.0e-6) |
| 88 | + { |
| 89 | + bmin.z(bmin.z() - size_estimate); |
| 90 | + bmax.z(bmax.z() + size_estimate); |
| 91 | + } |
| 92 | + |
| 93 | + Point center = bmin + dir/2.0 * scale; |
| 94 | + |
| 95 | + // Create glyphs |
| 96 | + widgets_.push_back(WidgetFactory::createSphere( |
| 97 | + idGenerator, |
| 98 | + widgetName(ArrowWidgetSection::SPHERE, widget_num, widget_iter), |
| 99 | + sphereRadius_ * scale, |
| 100 | + sphereCol.toString(), |
| 101 | + bmin, |
| 102 | + bmin, |
| 103 | + bbox, |
| 104 | + resolution)); |
| 105 | + widgets_[0]->setToTranslate(); |
| 106 | + |
| 107 | + if(show_as_vector) |
| 108 | + { |
| 109 | + // Starts the cylinder position closer to the surface of the sphere |
| 110 | + Point cylinderStart = bmin + 0.75 * (dir * scale * sphereRadius_); |
| 111 | + |
| 112 | + widgets_.push_back(WidgetFactory::createCylinder( |
| 113 | + idGenerator, |
| 114 | + widgetName(ArrowWidgetSection::CYLINDER, widget_num, widget_iter), |
| 115 | + cylinderRadius_ * scale, |
| 116 | + deflCol_.toString(), |
| 117 | + cylinderStart, |
| 118 | + center, |
| 119 | + bmin, |
| 120 | + bbox, |
| 121 | + resolution)); |
| 122 | + widgets_[1]->setToTranslate(); |
| 123 | + |
| 124 | + widgets_.push_back(WidgetFactory::createCone( |
| 125 | + idGenerator, |
| 126 | + widgetName(ArrowWidgetSection::CONE, widget_num, widget_iter), |
| 127 | + coneRadius_ * scale, |
| 128 | + deflCol_.toString(), |
| 129 | + center, |
| 130 | + bmax, |
| 131 | + bmin, |
| 132 | + bbox, |
| 133 | + true, |
| 134 | + resolution)); |
| 135 | + widgets_[2]->setToRotate(); |
| 136 | + |
| 137 | + Point diskPos = bmin + dir * scale * diskDistFromCenter_; |
| 138 | + Point dp1 = diskPos - diskWidth_ * dir * scale; |
| 139 | + Point dp2 = diskPos + diskWidth_ * dir * scale; |
| 140 | + widgets_.push_back(WidgetFactory::createDisk( |
| 141 | + idGenerator, |
| 142 | + widgetName(ArrowWidgetSection::DISK, widget_num, widget_iter), |
| 143 | + diskRadius_ * scale, |
| 144 | + resizeCol_.toString(), |
| 145 | + dp1, |
| 146 | + dp2, |
| 147 | + bmin, |
| 148 | + bbox, |
| 149 | + resolution)); |
| 150 | + Vector flipVec = dir.getArbitraryTangent().normal(); |
| 151 | + widgets_[3]->setToScale(flipVec); |
| 152 | + } |
| 153 | + |
| 154 | + std::vector<std::string> geom_ids; |
| 155 | + for(int i = 0; i < 1 + 3*show_as_vector; i++) |
| 156 | + geom_ids.push_back(widgets_[i]->uniqueID()); |
| 157 | + |
| 158 | + for(int i = 0; i < 1 + 3*show_as_vector; i++) |
| 159 | + { |
| 160 | + widgets_[i]->connectedIds_ = geom_ids; |
| 161 | + addToList(widgets_[i]); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +bool ArrowWidget::isVector() |
| 166 | +{ |
| 167 | + return isVector_; |
| 168 | +} |
| 169 | + |
| 170 | +std::string ArrowWidget::widgetName(size_t i, size_t id, size_t iter) |
| 171 | +{ |
| 172 | + return "ArrowWidget(" + std::to_string(i) + ")" + "(" + std::to_string(id) + ")" + |
| 173 | + "(" + std::to_string(iter) + ")"; |
| 174 | +} |
0 commit comments