1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
27 //----------------------------------------------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------------------------------------------
30 #include "OsirixParser.h"
34 OsirixParser::OsirixParser(std::string xsdfile, double* spacing, int* extent)
40 OSIRIX_IMAGES = "Images";
41 OSIRIX_ARRAY = "array";
42 OSIRIX_IMAGEINDEX = "ImageIndex";
43 OSIRIX_INTEGER = "integer";
44 OSIRIX_NUMBEROFROIS = "NumberOfROIs";
46 OSIRIX_POINT_MM = "Point_mm";
47 OSIRIX_POINT_PX = "Point_px";
48 OSIRIX_STRING = "string";
52 _spacing[0] = spacing[0];
53 _spacing[1] = spacing[1];
54 _spacing[2] = spacing[2];
62 _extent[0] = extent[0];
63 _extent[1] = extent[1];
64 _extent[2] = extent[2];
65 _extent[3] = extent[3];
66 _extent[4] = extent[4];
67 _extent[5] = extent[5];
80 CREACONTOUR = "--CreaContour--";
81 CREACONTOUR_VERSION = "Version 1.0.2";
82 CREACONTOUR_IMAGEDIMENSIONS = "ImageDimensions";
83 CREACONTOUR_IMAGESPACING = "ImageSpacing";
84 CREACONTOUR_NUMBEROFCONTOURS = "NumberOfContours";
86 CREACONTOUR_INSTANT = "Instant";
87 CREACONTOUR_TYPEMODEL = "TypeModel";
88 CREACONTOUR_NUMBEROFCONTROLPOINTS = "NumberOfControlPoints";
89 CREACONTOUR_TYPEVIEW = "TypeView";
91 CREACONTOUR_NUMBEROFCONTOURSSTATIC = "NumberOfContoursStatic";
93 TEMPIMPORTOSIRIXFILE = "";
97 char currentPath[_MAX_PATH];
98 GetModuleFileName(NULL, currentPath, _MAX_PATH);
99 TEMPIMPORTOSIRIXFILE = currentPath;
101 TEMPIMPORTOSIRIXFILE = TEMPIMPORTOSIRIXFILE.substr(0,TEMPIMPORTOSIRIXFILE.find_last_of("\\"));
103 TEMPIMPORTOSIRIXFILE.append("\\data\\TEMPIMPORTOSIRIXFILE.roi");
107 pPath = getenv ("HOME");
110 TEMPIMPORTOSIRIXFILE.append(pPath);
112 TEMPIMPORTOSIRIXFILE.append(".");
114 TEMPIMPORTOSIRIXFILE.append("/.creaContourDataTemp/TEMPIMPORTOSIRIXFILE.roi");
118 OsirixParser::~OsirixParser(){
122 int OsirixParser::ParseFile(const char* xmlfile){
124 xercesc::DOMDocument *doc;
128 XMLPlatformUtils::Initialize();
130 catch (const XMLException& toCatch) {
131 char* message = XMLString::transcode(toCatch.getMessage());
132 cout << "Error during initialization! :\n"
134 XMLString::release(&message);
138 XercesDOMParser* OsirixParser = new XercesDOMParser();
141 OsirixParser->setDoNamespaces(true);
142 OsirixParser->setDoSchema(true);
143 OsirixParser->setValidationScheme(XercesDOMParser::Val_Always );
144 OsirixParser->setExternalNoNamespaceSchemaLocation(XMLString::transcode(schema.c_str()));
145 OsirixParser->setValidationSchemaFullChecking(true);
146 OsirixParser->setValidationConstraintFatal(true);
147 OsirixParser->setExitOnFirstFatalError(true);
150 ErrorHandler* errHandler = (ErrorHandler*) new OsirixParserErrorHandler();
152 OsirixParser->setErrorHandler(errHandler);
156 OsirixParser->parse(xmlfile);
159 if(OsirixParser->getErrorCount() > 0){
161 errorline = ((OsirixParserErrorHandler*)OsirixParser->getErrorHandler())->getErrorMsg();
172 catch (const XMLException& toCatch) {
173 char* message = XMLString::transcode(toCatch.getMessage());
174 cout << "Exception message is: \n"
176 XMLString::release(&message);
181 catch (const DOMException& toCatch) {
182 char* message = XMLString::transcode(toCatch.msg);
183 cout << "Exception message is: \n"
185 XMLString::release(&message);
191 cout << "Unexpected Exception \n" ;
197 std::cout<<"parsing document..."<<std::endl;
200 doc = OsirixParser->getDocument();
201 // DOMNodeList* list = doc->getChildNodes();
202 getUserData(doc->getDocumentElement());
205 saveCreaContoursFormat();
216 void OsirixParser::setErrorLine(DOMNodeList* list){
218 DOMNode* node = list->item(0);
219 this->errorline = XMLString::transcode(node->getTextContent());
222 DOMNode* OsirixParser::getLastNode(DOMNodeList* list){
226 /*for(int i = list->getLength()-1; i >= 0 ;i--){
228 node = list->item(i);
230 if(node->getNodeType() == DOMNode::ELEMENT_NODE){
235 node = list->item(list->getLength()-1);
239 if(node->getChildNodes()->getLength()>0){
240 return getLastNode(node->getChildNodes());
246 void OsirixParser::getUserData(DOMElement* element){
247 parseOSIRIX_DICT(element->getElementsByTagName(XMLString::transcode(OSIRIX_DICT)));
250 void OsirixParser::parseOSIRIX_DICT(DOMNodeList* list){
252 DOMNode* node, *childnode, *childnode1, *childarray;
253 std::string point_mm, point_px, osirixinteger, imageindex, temp;
254 DOMNodeList* childlist;
255 point_mm = OSIRIX_POINT_MM;
256 point_px = OSIRIX_POINT_PX;
257 imageindex = OSIRIX_IMAGEINDEX;
258 osirixinteger = OSIRIX_INTEGER;
260 std::string osirixstring = OSIRIX_STRING;
261 std::string osirixname = OSIRIX_NAME;
264 for(i = 0; i < (int)(list->getLength()); i++){
265 node = list->item(i);
266 childlist = node->getChildNodes();
267 for(j = 0; j < (int)(childlist->getLength());j++){
268 childnode = childlist->item(j);
269 temp = XMLString::transcode(childnode->getTextContent());
272 if(point_mm.compare(temp)==0){
273 childarray = childlist->item(j+2);
274 //temp = XMLString::transcode(childarray->getNodeName());
276 parseOSIRIX_POINT_MM(childarray->getChildNodes());
278 }else if(point_px.compare(temp)==0){
279 childarray = childlist->item(j+2);
280 //temp = XMLString::transcode(childarray->getNodeName());
282 parseOSIRIX_POINT_PX(childarray->getChildNodes());
284 }else if(imageindex.compare(temp) == 0){
285 childnode1 = childlist->item(j+2);
286 if(childnode1 != NULL && osirixinteger.compare(XMLString::transcode(childnode1->getNodeName())) == 0){
287 _imageindex = atoi(XMLString::transcode(childnode1->getTextContent()));
289 } else if(osirixname.compare(temp) == 0) {
290 // keep information about the name of the ROI
291 childnode1 = childlist->item(j+2);
292 if(childnode1 != NULL && osirixstring.compare(XMLString::transcode(childnode1->getNodeName())) == 0){
293 char* roiname = XMLString::transcode(childnode1->getTextContent());
294 _roiname = string(roiname);
301 void OsirixParser::parseOSIRIX_POINT_MM(DOMNodeList* list){
302 int i, stringfound0, stringfound1;
304 string temp, osirix_string, numx, numy, numz;
305 vector<double>* vectorx;
306 vector<double>* vectory;
307 vector<double>* vectorz;
311 vectorx = new vector<double>;
312 vectory = new vector<double>;
313 vectorz = new vector<double>;
315 osirix_string = OSIRIX_STRING;
317 for(i = 0; i < (int)(list->getLength()); i++){
318 node = list->item(i);
319 if(osirix_string.compare(XMLString::transcode(node->getNodeName()))==0){
320 temp = XMLString::transcode(node->getTextContent());
322 stringfound0 = temp.find(",",0);
323 numx = temp.substr(1, stringfound0-1);
325 stringfound1 = temp.find(",",stringfound0+1);
326 numy = temp.substr(stringfound0+1, stringfound1-stringfound0-1);
328 stringfound0 = temp.find(")",stringfound1+1);
329 numz = temp.substr(stringfound1+1, stringfound0-stringfound1-1);
331 x = atof(numx.c_str());
332 y = atof(numy.c_str());
333 z = atof(numz.c_str());
335 vectorx->push_back(x);
336 vectory->push_back(y);
337 vectorz->push_back(z);
340 if(vectorx->size() > 0){
341 vectorxyz.push_back(*vectorx);
342 vectorxyz.push_back(*vectory);
343 vectorxyz.push_back(*vectorz);
344 contoursmapMM.insert(pair<int, vectorXYZ>(contoursmapMM.size(), vectorxyz));
345 contoursnameMM.insert(pair<int, string>(contoursnameMM.size(), _roiname));
349 void OsirixParser::parseOSIRIX_POINT_PX(DOMNodeList* list){
350 int i, stringfound0, stringfound1;
352 string temp, osirix_string, numx, numy;
353 vector<double>* vectorx;
354 vector<double>* vectory;
355 vector<double>* vectorz;
359 vectorx = new vector<double>;
360 vectory = new vector<double>;
361 vectorz = new vector<double>;
363 osirix_string = OSIRIX_STRING;
365 for(i = 0; i < (int)(list->getLength()); i++){
366 node = list->item(i);
367 if(osirix_string.compare(XMLString::transcode(node->getNodeName()))==0){
368 temp = XMLString::transcode(node->getTextContent());
370 stringfound0 = temp.find(",",0);
371 numx = temp.substr(1, stringfound0-1);
373 stringfound1 = temp.find(")",stringfound0+1);
374 numy = temp.substr(stringfound0+1, stringfound1-stringfound0-1);
376 x = atof(numx.c_str());
377 y = atof(numy.c_str());
379 vectorx->push_back(x);
380 vectory->push_back(y);
381 vectorz->push_back(_imageindex);
384 if(vectorx->size() > 0){
385 vectorxyz.push_back(*vectorx);
386 vectorxyz.push_back(*vectory);
387 vectorxyz.push_back(*vectorz);
388 contoursmapPX.insert(pair<int, vectorXYZ>(contoursmapPX.size(), vectorxyz));
389 contoursnamePX.insert(pair<int, string>(contoursnamePX.size(), _roiname));
395 void OsirixParser::getData(DOMNodeList* list, std::vector<std::string>& vect, std::string tagname){
397 for(int i = 0; i < (int)(list->getLength()); i++){
398 DOMNode* node = list->item(i);
399 if(tagname.compare(XMLString::transcode(node->getNodeName()))==0){
400 std::cout<<"NODENAME "<<XMLString::transcode(node->getTextContent())<<std::endl;
401 vect.push_back(XMLString::transcode(node->getTextContent()));
407 void OsirixParser::saveCreaContoursFormat(){
408 FILE *pFile=fopen(TEMPIMPORTOSIRIXFILE.c_str(),"w+");
412 writeContours(pFile);
413 writeContoursStatic(pFile);
418 void OsirixParser::writeContoursStatic(FILE* pFile){
419 fprintf(pFile, CREACONTOUR_IMAGEDIMENSIONS);
420 fprintf(pFile, " %d %d %d\n", _extent[1] - _extent[0],_extent[3] - _extent[2], _extent[5] - _extent[4]);
421 fprintf(pFile, CREACONTOUR_IMAGESPACING);
422 fprintf(pFile, " %f %f %f\n", _spacing[0], _spacing[1], _spacing[2]);
423 fprintf(pFile, CREACONTOUR_NUMBEROFCONTOURSSTATIC);
424 fprintf(pFile, " 0\n");
427 void OsirixParser::writeContours(FILE* pFile){
429 map<int, vectorXYZ>::iterator itPX;
430 vector<double> vectx, vecty, vectz;
432 int dimz = 0, dimy = 0;
435 dimz = _extent[5] - _extent[4] + 1;
436 dimy = _extent[3] - _extent[2] + 1;
439 /*for (itMM = contoursmapMM.begin(), itPX = contoursmapPX.begin();
440 itMM != contoursmapMM.end(), itPX != contoursmapPX.end();
442 for (itPX = contoursmapPX.begin(); itPX != contoursmapPX.end(); itPX++ ){
444 vectx = ((*itPX).second)[0];
445 vecty = ((*itPX).second)[1];
446 vectz = ((*itPX).second)[2];
450 valuez = (int) vectz[0] / _spacing[2];
452 fprintf(pFile, CREACONTOUR_INSTANT);
453 fprintf(pFile, " 1 %d 1 1 1 1\n",dimz - valuez);
454 fprintf(pFile, CREACONTOUR_TYPEMODEL);
455 fprintf(pFile, " 1\n");
456 fprintf(pFile, CREACONTOUR_NUMBEROFCONTROLPOINTS);
457 fprintf(pFile, " %d\n", vectz.size());
458 for(i = 0; i < vectx.size(); i++){
459 fprintf(pFile, "%f %f 900.00\n", vectx[i]/ _spacing[0], dimy - vecty[i]/ _spacing[1]);
460 // fprintf(pFile, "%f %f %f\n", vectx[i] , vecty[i], vectz[i]);
462 fprintf(pFile, CREACONTOUR_TYPEVIEW);
463 fprintf(pFile, " 1\n");
469 void OsirixParser::writeHeader(FILE* pFile){
472 fprintf(pFile, CREACONTOUR);
473 fprintf(pFile, "\n");
474 fprintf(pFile, CREACONTOUR_VERSION);
475 fprintf(pFile, "\n");
476 fprintf(pFile, CREACONTOUR_IMAGEDIMENSIONS);
477 fprintf(pFile, " %d %d %d\n", _extent[1] - _extent[0],_extent[3] - _extent[2], _extent[5] - _extent[4]);
478 fprintf(pFile, CREACONTOUR_IMAGESPACING);
479 fprintf(pFile, " %f %f %f\n", _spacing[0], _spacing[1], _spacing[2]);
480 fprintf(pFile, CREACONTOUR_NUMBEROFCONTOURS);
481 fprintf(pFile, " %d\n", contoursmapPX.size());
488 std::string OsirixParser::getContoursFileName(){
489 return TEMPIMPORTOSIRIXFILE;
493 ** The Error Handler's interface implementation
497 ** Default Constructor
499 OsirixParserErrorHandler::OsirixParserErrorHandler()
507 ** Desctructor by defect
509 OsirixParserErrorHandler::~OsirixParserErrorHandler(){
512 void OsirixParserErrorHandler::warning (const SAXParseException &exc){
515 void OsirixParserErrorHandler::error (const SAXParseException &exc){
517 errormsg = "Column ";
518 // errormsg += itoa(exc.getColumnNumber(),c,10);
520 sprintf_s(c,"%d",(int)(exc.getColumnNumber()));
522 sprintf(c,"%d",(int)(exc.getColumnNumber()));
524 errormsg += std::string(c);
526 errormsg += " Line ";
527 // errormsg += itoa(exc.getLineNumber(),c,10);
529 sprintf_s(c,"%d",(int)(exc.getLineNumber()));
531 sprintf(c,"%d",(int)(exc.getLineNumber()));
533 errormsg += std::string(c);
536 errormsg += XMLString::transcode(exc.getMessage());
540 void OsirixParserErrorHandler::fatalError (const SAXParseException &exc){
542 errormsg = "Column ";
543 // errormsg += itoa(exc.getColumnNumber(),c,10);
545 sprintf_s(c,"%d",(int)(exc.getColumnNumber()));
547 sprintf(c,"%d",(int)(exc.getColumnNumber()));
549 errormsg += std::string(c);
551 errormsg += " Line ";
552 // errormsg += itoa(exc.getLineNumber(),c,10);
554 sprintf_s(c,"%d",(int)(exc.getLineNumber()));
556 sprintf(c,"%d",(int)(exc.getLineNumber()));
558 errormsg += std::string(c);
561 errormsg += XMLString::transcode(exc.getMessage());
564 void OsirixParserErrorHandler::resetErrors (){
567 std::string OsirixParserErrorHandler::getErrorMsg(){
568 return this->errormsg;