00001 #include "Macrodefs.h"
00002 #include "Shared/Measures.h"
00003
00004 #include "Sketch.h"
00005 #include "BaseData.h"
00006 #include "ShapeRoot.h"
00007 #include "ShapePoint.h"
00008 #include "SketchDataRoot.h"
00009 #include "ShapeSpace.h"
00010
00011 using namespace std;
00012
00013 namespace DualCoding {
00014
00015 BoundingBox::BoundingBox(const BoundingBox &b1, const BoundingBox &b2) :
00016 xmin(std::min(b1.xmin,b2.xmin)), ymin(std::min(b1.ymin,b2.ymin)),
00017 xmax(std::max(b1.xmax,b2.xmax)), ymax(std::max(b1.ymax,b2.ymax)) {}
00018
00019 BoundingBox::BoundingBox(const std::vector<ShapeRoot> &vec) :
00020 xmin(0), ymin(0), xmax(0), ymax(0) {
00021 if ( vec.size() > 0 ) {
00022 *this = vec[0]->getBoundingBox();
00023 for ( size_t i = 1; i<vec.size(); i++ )
00024 *this = BoundingBox(*this,vec[i]->getBoundingBox());
00025 }
00026 }
00027
00028 std::ostream& operator<< (std::ostream& out, const BoundingBox &b) {
00029 out << "BoundingBox(" << b.xmin << "," << b.ymin << ","
00030 << b.xmax << "," << b.ymax << ")";
00031 return out;
00032 }
00033
00034
00035 BaseData::BaseData(ShapeSpace& _space, ShapeType_t _type, int _parentId) :
00036 space(&_space), name(data_name(_type)), type(_type),
00037 id(0), parentId(_parentId), lastMatchId(0),
00038 refcount(0), viewable(true),
00039 color_rgb((ProjectInterface::getNumColors() != -1U) ? ProjectInterface::getColorRGB(1) : rgb(0,0,255)),
00040 confidence(1),
00041 mobile(false),
00042 rendering_sketch(NULL)
00043 {};
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 BaseData::BaseData(const BaseData& other)
00059 : space(other.space), name(other.name), type(other.type),
00060 id(0), parentId(other.parentId), lastMatchId(other.lastMatchId),
00061 refcount(0), viewable(other.viewable),
00062 color_rgb(other.color_rgb),
00063 confidence(other.confidence),
00064 mobile(other.mobile),
00065 rendering_sketch(NULL)
00066 {
00067
00068 };
00069
00070
00071 BaseData::~BaseData(void) {
00072 if ( rendering_sketch != NULL )
00073 delete rendering_sketch;
00074 }
00075
00076 Shape<PointData> BaseData::getCentroidPtShape() const {
00077 PointData *pt = new PointData(*space,getCentroid());
00078 pt->inheritFrom(*this);
00079 return Shape<PointData>(pt);
00080 }
00081
00082 BaseData& BaseData::operator=(const BaseData& other) {
00083
00084
00085
00086
00087 space = other.space ? &(*other.space) : NULL;
00088 name = other.name;
00089 type = other.type;
00090 id = other.id;
00091 parentId = other.parentId;
00092 lastMatchId = other.lastMatchId;
00093 refcount = other.refcount;
00094 viewable = other.viewable;
00095 color_rgb = other.color_rgb;
00096 confidence = other.confidence;
00097 mobile = other.mobile;
00098 rendering_sketch = other.rendering_sketch ? &(*rendering_sketch) : NULL;
00099 return *this;
00100 }
00101
00102 void BaseData::inheritFrom(const BaseData &parent) {
00103 setParentId(parent.getViewableId());
00104 setColor(parent.getColor());
00105 }
00106
00107 void BaseData::inheritFrom(const ShapeRoot &parent) {
00108 setParentId(parent->getViewableId());
00109 setColor(parent->getColor());
00110 }
00111
00112 void BaseData::inheritFrom(const SketchDataRoot &parent) {
00113 setParentId(parent.getViewableId());
00114 setColor(parent.getColor());
00115 }
00116
00117 void BaseData::V(std::string const &_name) {
00118 setViewable(true);
00119 if ( !_name.empty() ) setName(_name);
00120 }
00121
00122 void BaseData::N(std::string const &_name) {
00123 setViewable(false);
00124 if ( !_name.empty() ) setName(_name);
00125 }
00126
00127 ReferenceFrameType_t BaseData::getRefFrameType() const {
00128 return space->getRefFrameType(); }
00129
00130
00131
00132
00133
00134 const char* BaseData::getTypeName() const { return data_name(type); }
00135
00136
00137 bool BaseData::isType(ShapeType_t this_type) const { return this_type == type; }
00138
00139
00140 bool BaseData::isSameTypeAs(const ShapeRoot& other) const {
00141 return((bool)(isType(other->type))); }
00142
00143
00144 bool BaseData::isSameColorAs(const ShapeRoot& other) const {
00145 return getColor() == other->getColor(); }
00146
00147 void BaseData::setColor(const std::string &color_name) {
00148 setColor(ProjectInterface::getColorRGB(color_name));
00149 }
00150
00151 void BaseData::setColor(const rgb &new_color) {
00152 color_rgb = new_color;
00153 if ( rendering_sketch != NULL )
00154 (*rendering_sketch)->setColor(new_color);
00155 }
00156
00157 void BaseData::setColor(const unsigned int color_index) {
00158 setColor(ProjectInterface::getColorRGB(color_index));
00159 }
00160
00161
00162 bool BaseData::getMobile() const { return mobile; }
00163
00164 void BaseData::setMobile(bool _mobile) { mobile = _mobile; }
00165
00166 void BaseData::deleteRendering() {
00167 delete rendering_sketch;
00168 rendering_sketch = NULL;
00169 }
00170
00171 Sketch<bool>& BaseData::getRendering() {
00172 if ( rendering_sketch != NULL )
00173 return *rendering_sketch;
00174 rendering_sketch = render();
00175 (*rendering_sketch)->setColor(getColor());
00176 (*rendering_sketch)->setParentId(id);
00177 (*rendering_sketch)->setName("render("+getName()+")");
00178 return *rendering_sketch;
00179 }
00180
00181
00182 void BaseData::increaseConfidence(int n, int maxConfidence) {
00183 confidence += n;
00184 if ( maxConfidence > 0 )
00185 confidence = std::min(confidence, maxConfidence);
00186 }
00187
00188 void BaseData::increaseConfidence(const BaseData& other, int maxConfidence) {
00189 increaseConfidence(other.getConfidence() > 0 ? other.getConfidence()+1 : 2, maxConfidence);
00190 }
00191
00192 void BaseData::increaseConfidence(const ShapeRoot& other, int maxConfidence) {
00193 increaseConfidence(other.getData(), maxConfidence);
00194 }
00195
00196 }