2 //----------------------------------------------------------------------------------------------------------------
3 // Class definition include
4 //----------------------------------------------------------------------------------------------------------------
5 #include "OsirixParser.h"
10 OsirixParser::OsirixParser(std::string xsdfile, double* spacing, int* extent)
16 OSIRIX_IMAGES = "Images";
17 OSIRIX_ARRAY = "array";
18 OSIRIX_IMAGEINDEX = "ImageIndex";
19 OSIRIX_INTEGER = "integer";
20 OSIRIX_NUMBEROFROIS = "NumberOfROIs";
22 OSIRIX_POINT_MM = "Point_mm";
23 OSIRIX_POINT_PX = "Point_px";
24 OSIRIX_STRING = "string";
27 _spacing[0] = spacing[0];
28 _spacing[1] = spacing[1];
29 _spacing[2] = spacing[2];
37 _extent[0] = extent[0];
38 _extent[1] = extent[1];
39 _extent[2] = extent[2];
40 _extent[3] = extent[3];
41 _extent[4] = extent[4];
42 _extent[5] = extent[5];
55 CREACONTOUR = "--CreaContour--";
56 CREACONTOUR_VERSION = "Version 1.0.2";
57 CREACONTOUR_IMAGEDIMENSIONS = "ImageDimensions";
58 CREACONTOUR_IMAGESPACING = "ImageSpacing";
59 CREACONTOUR_NUMBEROFCONTOURS = "NumberOfContours";
61 CREACONTOUR_INSTANT = "Instant";
62 CREACONTOUR_TYPEMODEL = "TypeModel";
63 CREACONTOUR_NUMBEROFCONTROLPOINTS = "NumberOfControlPoints";
64 CREACONTOUR_TYPEVIEW = "TypeView";
66 CREACONTOUR_NUMBEROFCONTOURSSTATIC = "NumberOfContoursStatic";
68 TEMPIMPORTOSIRIXFILE = "";
72 char currentPath[_MAX_PATH];
73 GetModuleFileName(NULL, currentPath, _MAX_PATH);
74 TEMPIMPORTOSIRIXFILE = currentPath;
76 TEMPIMPORTOSIRIXFILE = TEMPIMPORTOSIRIXFILE.substr(0,TEMPIMPORTOSIRIXFILE.find_last_of("\\"));
78 TEMPIMPORTOSIRIXFILE.append("\\data\\TEMPIMPORTOSIRIXFILE.roi");
82 pPath = getenv ("HOME");
85 TEMPIMPORTOSIRIXFILE.append(pPath);
87 TEMPIMPORTOSIRIXFILE.append(".");
89 TEMPIMPORTOSIRIXFILE.append("/.creaContourDataTemp/TEMPIMPORTOSIRIXFILE.roi");
93 OsirixParser::~OsirixParser(){
97 int OsirixParser::ParseFile(const char* xmlfile){
99 xercesc::DOMDocument *doc;
103 XMLPlatformUtils::Initialize();
105 catch (const XMLException& toCatch) {
106 char* message = XMLString::transcode(toCatch.getMessage());
107 cout << "Error during initialization! :\n"
109 XMLString::release(&message);
113 XercesDOMParser* OsirixParser = new XercesDOMParser();
116 OsirixParser->setDoNamespaces(true);
117 OsirixParser->setDoSchema(true);
118 OsirixParser->setValidationScheme(XercesDOMParser::Val_Always );
119 OsirixParser->setExternalNoNamespaceSchemaLocation(XMLString::transcode(schema.c_str()));
120 OsirixParser->setValidationSchemaFullChecking(true);
121 OsirixParser->setValidationConstraintFatal(true);
122 OsirixParser->setExitOnFirstFatalError(true);
125 ErrorHandler* errHandler = (ErrorHandler*) new OsirixParserErrorHandler();
127 OsirixParser->setErrorHandler(errHandler);
131 OsirixParser->parse(xmlfile);
134 if(OsirixParser->getErrorCount() > 0){
136 errorline = ((OsirixParserErrorHandler*)OsirixParser->getErrorHandler())->getErrorMsg();
147 catch (const XMLException& toCatch) {
148 char* message = XMLString::transcode(toCatch.getMessage());
149 cout << "Exception message is: \n"
151 XMLString::release(&message);
156 catch (const DOMException& toCatch) {
157 char* message = XMLString::transcode(toCatch.msg);
158 cout << "Exception message is: \n"
160 XMLString::release(&message);
166 cout << "Unexpected Exception \n" ;
172 std::cout<<"parsing document..."<<std::endl;
175 doc = OsirixParser->getDocument();
176 // DOMNodeList* list = doc->getChildNodes();
177 getUserData(doc->getDocumentElement());
180 saveCreaContoursFormat();
191 void OsirixParser::setErrorLine(DOMNodeList* list){
193 DOMNode* node = list->item(0);
194 this->errorline = XMLString::transcode(node->getTextContent());
197 DOMNode* OsirixParser::getLastNode(DOMNodeList* list){
201 /*for(int i = list->getLength()-1; i >= 0 ;i--){
203 node = list->item(i);
205 if(node->getNodeType() == DOMNode::ELEMENT_NODE){
210 node = list->item(list->getLength()-1);
214 if(node->getChildNodes()->getLength()>0){
215 return getLastNode(node->getChildNodes());
221 void OsirixParser::getUserData(DOMElement* element){
222 parseOSIRIX_DICT(element->getElementsByTagName(XMLString::transcode(OSIRIX_DICT)));
225 void OsirixParser::parseOSIRIX_DICT(DOMNodeList* list){
227 DOMNode* node, *childnode, *childnode1, *childarray;
228 std::string point_mm, point_px, osirixinteger, imageindex, temp;
229 DOMNodeList* childlist;
230 point_mm = OSIRIX_POINT_MM;
231 point_px = OSIRIX_POINT_PX;
232 imageindex = OSIRIX_IMAGEINDEX;
233 osirixinteger = OSIRIX_INTEGER;
237 for(i = 0; i < (int)(list->getLength()); i++){
238 node = list->item(i);
239 childlist = node->getChildNodes();
240 for(j = 0; j < (int)(childlist->getLength());j++){
241 childnode = childlist->item(j);
242 temp = XMLString::transcode(childnode->getTextContent());
245 if(point_mm.compare(temp)==0){
246 childarray = childlist->item(j+2);
247 //temp = XMLString::transcode(childarray->getNodeName());
249 parseOSIRIX_POINT_MM(childarray->getChildNodes());
251 }else if(point_px.compare(temp)==0){
252 childarray = childlist->item(j+2);
253 //temp = XMLString::transcode(childarray->getNodeName());
255 parseOSIRIX_POINT_PX(childarray->getChildNodes());
257 }else if(imageindex.compare(temp) == 0){
258 childnode1 = childlist->item(j+2);
259 if(childnode1 != NULL && osirixinteger.compare(XMLString::transcode(childnode1->getNodeName())) == 0){
260 _imageindex = atoi(XMLString::transcode(childnode1->getTextContent()));
267 void OsirixParser::parseOSIRIX_POINT_MM(DOMNodeList* list){
268 int i, stringfound0, stringfound1;
270 string temp, osirix_string, numx, numy, numz;
271 vector<double>* vectorx;
272 vector<double>* vectory;
273 vector<double>* vectorz;
277 vectorx = new vector<double>;
278 vectory = new vector<double>;
279 vectorz = new vector<double>;
281 osirix_string = OSIRIX_STRING;
283 for(i = 0; i < (int)(list->getLength()); i++){
284 node = list->item(i);
285 if(osirix_string.compare(XMLString::transcode(node->getNodeName()))==0){
286 temp = XMLString::transcode(node->getTextContent());
288 stringfound0 = temp.find(",",0);
289 numx = temp.substr(1, stringfound0-1);
291 stringfound1 = temp.find(",",stringfound0+1);
292 numy = temp.substr(stringfound0+1, stringfound1-stringfound0-1);
294 stringfound0 = temp.find(")",stringfound1+1);
295 numz = temp.substr(stringfound1+1, stringfound0-stringfound1-1);
297 x = atof(numx.c_str());
298 y = atof(numy.c_str());
299 z = atof(numz.c_str());
301 vectorx->push_back(x);
302 vectory->push_back(y);
303 vectorz->push_back(z);
306 if(vectorx->size() > 0){
307 vectorxyz.push_back(*vectorx);
308 vectorxyz.push_back(*vectory);
309 vectorxyz.push_back(*vectorz);
310 contoursmapMM.insert(pair<int, vectorXYZ>(contoursmapMM.size(), vectorxyz));
314 void OsirixParser::parseOSIRIX_POINT_PX(DOMNodeList* list){
315 int i, stringfound0, stringfound1;
317 string temp, osirix_string, numx, numy;
318 vector<double>* vectorx;
319 vector<double>* vectory;
320 vector<double>* vectorz;
324 vectorx = new vector<double>;
325 vectory = new vector<double>;
326 vectorz = new vector<double>;
328 osirix_string = OSIRIX_STRING;
330 for(i = 0; i < (int)(list->getLength()); i++){
331 node = list->item(i);
332 if(osirix_string.compare(XMLString::transcode(node->getNodeName()))==0){
333 temp = XMLString::transcode(node->getTextContent());
335 stringfound0 = temp.find(",",0);
336 numx = temp.substr(1, stringfound0-1);
338 stringfound1 = temp.find(")",stringfound0+1);
339 numy = temp.substr(stringfound0+1, stringfound1-stringfound0-1);
341 x = atof(numx.c_str());
342 y = atof(numy.c_str());
344 vectorx->push_back(x);
345 vectory->push_back(y);
346 vectorz->push_back(_imageindex);
349 if(vectorx->size() > 0){
350 vectorxyz.push_back(*vectorx);
351 vectorxyz.push_back(*vectory);
352 vectorxyz.push_back(*vectorz);
353 contoursmapPX.insert(pair<int, vectorXYZ>(contoursmapPX.size(), vectorxyz));
359 void OsirixParser::getData(DOMNodeList* list, std::vector<std::string>& vect, std::string tagname){
361 for(int i = 0; i < (int)(list->getLength()); i++){
362 DOMNode* node = list->item(i);
363 if(tagname.compare(XMLString::transcode(node->getNodeName()))==0){
364 std::cout<<"NODENAME "<<XMLString::transcode(node->getTextContent())<<std::endl;
365 vect.push_back(XMLString::transcode(node->getTextContent()));
371 void OsirixParser::saveCreaContoursFormat(){
372 FILE *pFile=fopen(TEMPIMPORTOSIRIXFILE.c_str(),"w+");
376 writeContours(pFile);
377 writeContoursStatic(pFile);
382 void OsirixParser::writeContoursStatic(FILE* pFile){
383 fprintf(pFile, CREACONTOUR_IMAGEDIMENSIONS);
384 fprintf(pFile, " %d %d %d\n", _extent[1] - _extent[0],_extent[3] - _extent[2], _extent[5] - _extent[4]);
385 fprintf(pFile, CREACONTOUR_IMAGESPACING);
386 fprintf(pFile, " %f %f %f\n", _spacing[0], _spacing[1], _spacing[2]);
387 fprintf(pFile, CREACONTOUR_NUMBEROFCONTOURSSTATIC);
388 fprintf(pFile, " 0\n");
391 void OsirixParser::writeContours(FILE* pFile){
393 map<int, vectorXYZ>::iterator itPX;
394 vector<double> vectx, vecty, vectz;
396 int dimz = 0, dimy = 0;
399 dimz = _extent[5] - _extent[4] + 1;
400 dimy = _extent[3] - _extent[2] + 1;
403 /*for (itMM = contoursmapMM.begin(), itPX = contoursmapPX.begin();
404 itMM != contoursmapMM.end(), itPX != contoursmapPX.end();
406 for (itPX = contoursmapPX.begin(); itPX != contoursmapPX.end(); itPX++ ){
408 vectx = ((*itPX).second)[0];
409 vecty = ((*itPX).second)[1];
410 vectz = ((*itPX).second)[2];
414 valuez = (int) vectz[0] / _spacing[2];
416 fprintf(pFile, CREACONTOUR_INSTANT);
417 fprintf(pFile, " 1 %d 1 1 1 1\n",dimz - valuez);
418 fprintf(pFile, CREACONTOUR_TYPEMODEL);
419 fprintf(pFile, " 1\n");
420 fprintf(pFile, CREACONTOUR_NUMBEROFCONTROLPOINTS);
421 fprintf(pFile, " %d\n", vectz.size());
422 for(i = 0; i < vectx.size(); i++){
423 fprintf(pFile, "%f %f 900.00\n", vectx[i]/ _spacing[0],dimy - vecty[i]/ _spacing[1]);
425 fprintf(pFile, CREACONTOUR_TYPEVIEW);
426 fprintf(pFile, " 1\n");
432 void OsirixParser::writeHeader(FILE* pFile){
435 fprintf(pFile, CREACONTOUR);
436 fprintf(pFile, "\n");
437 fprintf(pFile, CREACONTOUR_VERSION);
438 fprintf(pFile, "\n");
439 fprintf(pFile, CREACONTOUR_IMAGEDIMENSIONS);
440 fprintf(pFile, " %d %d %d\n", _extent[1] - _extent[0],_extent[3] - _extent[2], _extent[5] - _extent[4]);
441 fprintf(pFile, CREACONTOUR_IMAGESPACING);
442 fprintf(pFile, " %f %f %f\n", _spacing[0], _spacing[1], _spacing[2]);
443 fprintf(pFile, CREACONTOUR_NUMBEROFCONTOURS);
444 fprintf(pFile, " %d\n", contoursmapPX.size());
451 std::string OsirixParser::getContoursFileName(){
452 return TEMPIMPORTOSIRIXFILE;
456 ** The Error Handler's interface implementation
460 ** Default Constructor
462 OsirixParserErrorHandler::OsirixParserErrorHandler()
470 ** Desctructor by defect
472 OsirixParserErrorHandler::~OsirixParserErrorHandler(){
475 void OsirixParserErrorHandler::warning (const SAXParseException &exc){
478 void OsirixParserErrorHandler::error (const SAXParseException &exc){
480 errormsg = "Column ";
481 // errormsg += itoa(exc.getColumnNumber(),c,10);
483 sprintf_s(c,"%d",(int)(exc.getColumnNumber()));
485 sprintf(c,"%d",(int)(exc.getColumnNumber()));
487 errormsg += std::string(c);
489 errormsg += " Line ";
490 // errormsg += itoa(exc.getLineNumber(),c,10);
492 sprintf_s(c,"%d",(int)(exc.getLineNumber()));
494 sprintf(c,"%d",(int)(exc.getLineNumber()));
496 errormsg += std::string(c);
499 errormsg += XMLString::transcode(exc.getMessage());
503 void OsirixParserErrorHandler::fatalError (const SAXParseException &exc){
505 errormsg = "Column ";
506 // errormsg += itoa(exc.getColumnNumber(),c,10);
508 sprintf_s(c,"%d",(int)(exc.getColumnNumber()));
510 sprintf(c,"%d",(int)(exc.getColumnNumber()));
512 errormsg += std::string(c);
514 errormsg += " Line ";
515 // errormsg += itoa(exc.getLineNumber(),c,10);
517 sprintf_s(c,"%d",(int)(exc.getLineNumber()));
519 sprintf(c,"%d",(int)(exc.getLineNumber()));
521 errormsg += std::string(c);
524 errormsg += XMLString::transcode(exc.getMessage());
527 void OsirixParserErrorHandler::resetErrors (){
530 std::string OsirixParserErrorHandler::getErrorMsg(){
531 return this->errormsg;