Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SceneText.cpp
1 #include "SceneText.hh"
2 #include <GLviewer/ContextWX.hh>
3 
4 #ifdef BIAS_HAVE_OPENGL
5 # include <Gui/biasgl.h>
6 #ifdef __APPLE__
7 # include <OpenGL/glu.h>
8 #else // __APPLE_
9 # include <GL/glu.h>
10 #endif // __APPLE__
11 # include <Utils/StatekeeperGL.hh>
12 #endif
13 
14 
15 using namespace std;
16 using namespace BIAS;
17 
18 
19 SceneText::SceneText(bool MakeDemoText): isInitialized_(false) {
20  NextIndex_ =0;
21  if (MakeDemoText) {
23  textContents_[NextIndex_]="Northern Star";
24  textColors_[NextIndex_]=RGBf(1.0f, 0.0f, 0.0f);
25  NextIndex_++ ;
26  }
27  //fontName_ = DrawTextGL_DEFAULT_FONTNAME;
28  fontName_ = "-*-helvetica-bold-r-normal--12-*-*-*-p-*-iso8859-1";
29  fontHeight_ = 12;
30  Context_ = NULL;
31 
32 }
33 
34 unsigned int SceneText::AddText(const HomgPoint3D &textPositions,const string& textContents,
35  const RGBf& colors)
36 {
37  textPositions_[NextIndex_]=textPositions;
38  textContents_[NextIndex_]=textContents;
39  textColors_[NextIndex_]=colors;
40  return NextIndex_++;
41 }
42 
43 
44 void SceneText::SetText(const std::vector<BIAS::HomgPoint3D>& textPositions,
45  const std::vector<std::string>& textContents,
46  const std::vector<BIAS::RGBf>& colors)
47 {
48  for (unsigned int i=0;i<textPositions.size();i++)
49  AddText(textPositions[i],textContents[i],colors[i]);
50 }
51 
52 
53 void SceneText::RemoveText(unsigned int i)
54 {
55 textPositions_.erase(i);
56 textContents_.erase(i);
57 textColors_.erase(i);
58 }
59 
60 
61 void SceneText::UpdateText(unsigned int index,const BIAS::HomgPoint3D& textPositions,
62  const std::string& textContents,
63  const BIAS::RGBf& colors )
64 {
65  BIASASSERT(textPositions_.find(index) != textPositions_.end());
66  textPositions_[index]=textPositions;
67  textContents_[index]=textContents;
68  textColors_[index]=colors;
69 
70 }
71 
72 
74 {
75 // #ifdef WIN32
76 // BIASWARNONCE("windows font selection mechanism not yet implmented here.");
77 // return;
78 // #endif
79 #ifdef BIAS_HAVE_OPENGL
80 
81  if (!isInitialized_) {
82 
83  void *handleDC = NULL;
84 #ifdef WIN32
85  BIASASSERT(Context_ != NULL);
86  ContextWX *CWX = dynamic_cast<ContextWX *>(Context_);
87  BIASASSERT(CWX != NULL);
88  handleDC = GetDC(HWND(CWX->GetHandle())); //Use CWX->GetHWDN()?
89 #endif
90 
91 #ifdef BIAS_HAVE_FONTGL
92  if (drawer_.InitFont(handleDC, fontName_, fontHeight_)!=0) {
93  cerr<<"init of font failed ..."<<endl;
94  return;
95  }
96 #else
97  BIASWARNONCE("FontGL is disabled in BIAS.");
98  return;
99 #endif
100  isInitialized_ = true;
101  }
102 
103  map<unsigned int,BIAS::HomgPoint3D>::iterator p3dit=textPositions_.begin();
104  map<unsigned int,std::string>::iterator textit=textContents_.begin();
105  map<unsigned int,BIAS::RGBf>::iterator colorit=textColors_.begin();
106 
107 
108  while (p3dit != textPositions_.end()) {
109  glColor3f(colorit->second[0], colorit->second[1], colorit->second[2]);
110 #ifdef BIAS_HAVE_FONTGL
111  drawer_.Print3D(textit->second, p3dit->second[0],
112  p3dit->second[1],p3dit->second[2]);
113 
114 
115 
116  //drawer_.Print3D(".", textPositions_[i][0],
117  // textPositions_[i][1], textPositions_[i][2]);
118  glBegin(GL_POINTS);
119  glVertex3f(p3dit->second[0], p3dit->second[1],p3dit->second[2]);
120  glEnd();
121 #endif
122  p3dit++;
123  textit++;
124  colorit++;
125  }
126 #endif
127 
128 }
129 
130 
131 
132 
133 
134 
135 #ifdef BIAS_HAVE_XML2
136 
137 int SceneText::XMLGetClassName(std::string& TopLevelTag,
138  double& Version) const {
139  TopLevelTag = "SceneText";
140  Version = 0.1;
141  return 0;
142 }
143 
144 int SceneText::XMLOut(const xmlNodePtr mainNode, XMLIO& XMLObject) const {
145  std::map<unsigned int,BIAS::HomgPoint3D>::const_iterator itP= textPositions_.begin();
146  std::map<unsigned int,std::string>::const_iterator itC= textContents_.begin();
147  std::map<unsigned int,BIAS::RGBf>::const_iterator itCC= textColors_.begin();
148  for (; itP!=textPositions_.end(); ) {
149  xmlNodePtr childNode = XMLObject.addChildNode(mainNode, "Text");
150 
151  XMLObject.addAttribute(childNode, "r", itCC->second[0]);
152  XMLObject.addAttribute(childNode, "g", itCC->second[1]);
153  XMLObject.addAttribute(childNode, "b", itCC->second[2]);
154 
155  XMLObject.addAttribute(childNode, "x", itP->second[0]);
156  XMLObject.addAttribute(childNode, "y", itP->second[1]);
157  XMLObject.addAttribute(childNode, "z", itP->second[2]);
158  XMLObject.addAttribute(childNode, "w", itP->second[3]);
159 
160  XMLObject.addContent(childNode, itC->second);
161  itP++;
162  itC++;
163  itCC++;
164  }
165  return 0;
166 }
167 
168 int SceneText::XMLIn(const xmlNodePtr Node, XMLIO& XMLObject) {
169  textColors_.clear();
170  textPositions_.clear();
171  textContents_.clear();
172  xmlNodePtr childNode = XMLObject.getFirstChild(Node);
173  while (!(childNode==NULL)) {
174  if (XMLObject.getNodeName(childNode)=="Text") {
175  RGBf thecolor;
176  HomgPoint3D theposition;
177 
178  thecolor[0] = XMLObject.getAttributeValueDouble(childNode, "r");
179  thecolor[1] = XMLObject.getAttributeValueDouble(childNode, "g");
180  thecolor[2] = XMLObject.getAttributeValueDouble(childNode, "b");
181 
182  theposition[0] = XMLObject.getAttributeValueDouble(childNode, "x");
183  theposition[1] = XMLObject.getAttributeValueDouble(childNode, "y");
184  theposition[2] = XMLObject.getAttributeValueDouble(childNode, "z");
185  theposition[3] = XMLObject.getAttributeValueDouble(childNode, "w");
186  string theContent = "___"+XMLObject.getNodeContentString(childNode);
187  AddText(theposition,theContent,thecolor);
188  cout<<"Found "<<theContent<<" at "<<theposition<<" with color "
189  <<thecolor<<endl;
190  }
191  childNode = XMLObject.getNextChild();
192  }
193  cout<<"Read "<<textColors_.size()<<" texts"<<endl;
194 
195  return 0;
196 }
197 
198 #endif // BIAS_HAVE_XML2
199 
200 
201 
202 
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
Definition: XMLIO.cpp:156
virtual int XMLGetClassName(std::string &TopLevelTag, double &Version) const
specialization of XML block name function
Definition: SceneText.cpp:137
void Print3D(const std::string &text, const float &xpos, const float &ypos, const float &zpos) const
Definition: DrawTextGL.cpp:275
xmlNodePtr getNextChild()
Get the next child of the parent specified in the last getFirstChild() call, the class remembers the ...
Definition: XMLIO.cpp:466
BIAS::DrawTextGL drawer_
Definition: SceneText.hh:81
unsigned int AddText(const BIAS::HomgPoint3D &textPositions, const std::string &textContents, const BIAS::RGBf &colors)
Definition: SceneText.cpp:34
void SetText(const std::vector< BIAS::HomgPoint3D > &textPositions, const std::vector< std::string > &textContents, const std::vector< BIAS::RGBf > &colors)
Definition: SceneText.cpp:44
void UpdateText(unsigned int, const BIAS::HomgPoint3D &textPositions, const std::string &textContents, const BIAS::RGBf &colors)
Definition: SceneText.cpp:61
void RemoveText(unsigned int)
Definition: SceneText.cpp:53
bool isInitialized_
Definition: SceneText.hh:90
int InitFont(void *handleDC, const std::string theFontname=std::string(DrawTextGL_DEFAULT_FONTNAME), const int fontHeight=DrawTextGL_DEFAULT_FONTSIZE)
set the font up
Definition: DrawTextGL.cpp:80
void addContent(const xmlNodePtr Node, const std::string &Content)
Add content to a node.
Definition: XMLIO.cpp:254
BIAS::Vector3< float > RGBf
Definition: RGB.hh:22
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
Context implementation for wxWidgets.
Definition: ContextWX.hh:31
xmlNodePtr addChildNode(const xmlNodePtr ParentNode, const std::string &NewNodeName)
Add a child node to an incoming node with the given name.
Definition: XMLIO.cpp:131
std::map< unsigned int, BIAS::HomgPoint3D > textPositions_
Definition: SceneText.hh:84
unsigned int NextIndex_
Definition: SceneText.hh:87
xmlNodePtr getFirstChild(const xmlNodePtr ParentNode)
Get the first child of a given parent, or NULL for no childs.
Definition: XMLIO.cpp:452
virtual int XMLIn(const xmlNodePtr Node, BIAS::XMLIO &XMLObject)
specialization of XML read function
Definition: SceneText.cpp:168
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
std::map< unsigned int, std::string > textContents_
Definition: SceneText.hh:85
virtual int XMLOut(const xmlNodePtr Node, BIAS::XMLIO &XMLObject) const
specialization of XML write function
Definition: SceneText.cpp:144
double getAttributeValueDouble(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:736
std::map< unsigned int, BIAS::RGBf > textColors_
Definition: SceneText.hh:86
std::string fontName_
Definition: SceneText.hh:88
RenderContextBase * Context_
Definition: SceneText.hh:91
std::string getNodeName(const xmlNodePtr Node) const
Get the name of a given Node.
Definition: XMLIO.cpp:543
virtual void Draw()
To do anything usefull, overload this method, assume context is ready and draw.
Definition: SceneText.cpp:73
std::string getNodeContentString(const xmlNodePtr Node) const
Get the content of a given Node.
Definition: XMLIO.cpp:554
class BIASGeometryBase_EXPORT HomgPoint3D