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];
99 wchar_t currentPath2[512];
100 long result = GetModuleFileName(NULL, currentPath2, _MAX_PATH);
101 // GetModuleFileName(NULL, currentPath, _MAX_PATH);
102 int ret = wcstombs ( currentPath, currentPath2, sizeof(pname) );
104 TEMPIMPORTOSIRIXFILE = currentPath;
106 TEMPIMPORTOSIRIXFILE = TEMPIMPORTOSIRIXFILE.substr(0,TEMPIMPORTOSIRIXFILE.find_last_of("\\"));
108 TEMPIMPORTOSIRIXFILE.append("\\data\\TEMPIMPORTOSIRIXFILE.roi");
112 pPath = getenv ("HOME");
115 TEMPIMPORTOSIRIXFILE.append(pPath);
117 TEMPIMPORTOSIRIXFILE.append(".");
119 TEMPIMPORTOSIRIXFILE.append("/.creaContourDataTemp/TEMPIMPORTOSIRIXFILE.roi");
123 OsirixParser::~OsirixParser(){
127 int OsirixParser::ParseFile(const char* xmlfile){
129 xercesc::DOMDocument *doc;
133 XMLPlatformUtils::Initialize();
135 catch (const XMLException& toCatch) {
136 char* message = XMLString::transcode(toCatch.getMessage());
137 cout << "Error during initialization! :\n"
139 XMLString::release(&message);
143 XercesDOMParser* OsirixParser = new XercesDOMParser();
146 OsirixParser->setDoNamespaces(true);
147 OsirixParser->setDoSchema(true);
148 OsirixParser->setValidationScheme(XercesDOMParser::Val_Always );
149 OsirixParser->setExternalNoNamespaceSchemaLocation(XMLString::transcode(schema.c_str()));
150 OsirixParser->setValidationSchemaFullChecking(true);
151 OsirixParser->setValidationConstraintFatal(true);
152 OsirixParser->setExitOnFirstFatalError(true);
155 ErrorHandler* errHandler = (ErrorHandler*) new OsirixParserErrorHandler();
157 OsirixParser->setErrorHandler(errHandler);
161 OsirixParser->parse(xmlfile);
164 if(OsirixParser->getErrorCount() > 0){
166 errorline = ((OsirixParserErrorHandler*)OsirixParser->getErrorHandler())->getErrorMsg();
177 catch (const XMLException& toCatch) {
178 char* message = XMLString::transcode(toCatch.getMessage());
179 cout << "Exception message is: \n"
181 XMLString::release(&message);
186 catch (const DOMException& toCatch) {
187 char* message = XMLString::transcode(toCatch.msg);
188 cout << "Exception message is: \n"
190 XMLString::release(&message);
196 cout << "Unexpected Exception \n" ;
202 std::cout<<"parsing document..."<<std::endl;
205 doc = OsirixParser->getDocument();
206 // DOMNodeList* list = doc->getChildNodes();
207 getUserData(doc->getDocumentElement());
210 saveCreaContoursFormat();
221 void OsirixParser::setErrorLine(DOMNodeList* list){
223 DOMNode* node = list->item(0);
224 this->errorline = XMLString::transcode(node->getTextContent());
227 DOMNode* OsirixParser::getLastNode(DOMNodeList* list){
231 /*for(int i = list->getLength()-1; i >= 0 ;i--){
233 node = list->item(i);
235 if(node->getNodeType() == DOMNode::ELEMENT_NODE){
240 node = list->item(list->getLength()-1);
244 if(node->getChildNodes()->getLength()>0){
245 return getLastNode(node->getChildNodes());
251 void OsirixParser::getUserData(DOMElement* element){
252 parseOSIRIX_DICT(element->getElementsByTagName(XMLString::transcode(OSIRIX_DICT)));
255 void OsirixParser::parseOSIRIX_DICT(DOMNodeList* list){
257 DOMNode* node, *childnode, *childnode1, *childarray;
258 std::string point_mm, point_px, osirixinteger, imageindex, temp;
259 DOMNodeList* childlist;
260 point_mm = OSIRIX_POINT_MM;
261 point_px = OSIRIX_POINT_PX;
262 imageindex = OSIRIX_IMAGEINDEX;
263 osirixinteger = OSIRIX_INTEGER;
265 std::string osirixstring = OSIRIX_STRING;
266 std::string osirixname = OSIRIX_NAME;
269 for(i = 0; i < (int)(list->getLength()); i++){
270 node = list->item(i);
271 childlist = node->getChildNodes();
272 for(j = 0; j < (int)(childlist->getLength());j++){
273 childnode = childlist->item(j);
274 temp = XMLString::transcode(childnode->getTextContent());
277 if(point_mm.compare(temp)==0){
278 childarray = childlist->item(j+2);
279 //temp = XMLString::transcode(childarray->getNodeName());
281 parseOSIRIX_POINT_MM(childarray->getChildNodes());
283 }else if(point_px.compare(temp)==0){
284 childarray = childlist->item(j+2);
285 //temp = XMLString::transcode(childarray->getNodeName());
287 parseOSIRIX_POINT_PX(childarray->getChildNodes());
289 }else if(imageindex.compare(temp) == 0){
290 childnode1 = childlist->item(j+2);
291 if(childnode1 != NULL && osirixinteger.compare(XMLString::transcode(childnode1->getNodeName())) == 0){
292 _imageindex = atoi(XMLString::transcode(childnode1->getTextContent()));
294 } else if(osirixname.compare(temp) == 0) {
295 // keep information about the name of the ROI
296 childnode1 = childlist->item(j+2);
297 if(childnode1 != NULL && osirixstring.compare(XMLString::transcode(childnode1->getNodeName())) == 0){
298 char* roiname = XMLString::transcode(childnode1->getTextContent());
299 _roiname = string(roiname);
306 void OsirixParser::parseOSIRIX_POINT_MM(DOMNodeList* list){
307 int i, stringfound0, stringfound1;
309 string temp, osirix_string, numx, numy, numz;
310 vector<double>* vectorx;
311 vector<double>* vectory;
312 vector<double>* vectorz;
316 vectorx = new vector<double>;
317 vectory = new vector<double>;
318 vectorz = new vector<double>;
320 osirix_string = OSIRIX_STRING;
322 for(i = 0; i < (int)(list->getLength()); i++){
323 node = list->item(i);
324 if(osirix_string.compare(XMLString::transcode(node->getNodeName()))==0){
325 temp = XMLString::transcode(node->getTextContent());
327 stringfound0 = temp.find(",",0);
328 numx = temp.substr(1, stringfound0-1);
330 stringfound1 = temp.find(",",stringfound0+1);
331 numy = temp.substr(stringfound0+1, stringfound1-stringfound0-1);
333 stringfound0 = temp.find(")",stringfound1+1);
334 numz = temp.substr(stringfound1+1, stringfound0-stringfound1-1);
336 x = atof(numx.c_str());
337 y = atof(numy.c_str());
338 z = atof(numz.c_str());
340 vectorx->push_back(x);
341 vectory->push_back(y);
342 vectorz->push_back(z);
345 if(vectorx->size() > 0){
346 vectorxyz.push_back(*vectorx);
347 vectorxyz.push_back(*vectory);
348 vectorxyz.push_back(*vectorz);
349 contoursmapMM.insert(pair<int, vectorXYZ>(contoursmapMM.size(), vectorxyz));
350 contoursnameMM.insert(pair<int, string>(contoursnameMM.size(), _roiname));
354 void OsirixParser::parseOSIRIX_POINT_PX(DOMNodeList* list){
355 int i, stringfound0, stringfound1;
357 string temp, osirix_string, numx, numy;
358 vector<double>* vectorx;
359 vector<double>* vectory;
360 vector<double>* vectorz;
364 vectorx = new vector<double>;
365 vectory = new vector<double>;
366 vectorz = new vector<double>;
368 osirix_string = OSIRIX_STRING;
370 for(i = 0; i < (int)(list->getLength()); i++){
371 node = list->item(i);
372 if(osirix_string.compare(XMLString::transcode(node->getNodeName()))==0){
373 temp = XMLString::transcode(node->getTextContent());
375 stringfound0 = temp.find(",",0);
376 numx = temp.substr(1, stringfound0-1);
378 stringfound1 = temp.find(")",stringfound0+1);
379 numy = temp.substr(stringfound0+1, stringfound1-stringfound0-1);
381 x = atof(numx.c_str());
382 y = atof(numy.c_str());
384 vectorx->push_back(x);
385 vectory->push_back(y);
386 vectorz->push_back(_imageindex);
389 if(vectorx->size() > 0){
390 vectorxyz.push_back(*vectorx);
391 vectorxyz.push_back(*vectory);
392 vectorxyz.push_back(*vectorz);
393 contoursmapPX.insert(pair<int, vectorXYZ>(contoursmapPX.size(), vectorxyz));
394 contoursnamePX.insert(pair<int, string>(contoursnamePX.size(), _roiname));
400 void OsirixParser::getData(DOMNodeList* list, std::vector<std::string>& vect, std::string tagname){
402 for(int i = 0; i < (int)(list->getLength()); i++){
403 DOMNode* node = list->item(i);
404 if(tagname.compare(XMLString::transcode(node->getNodeName()))==0){
405 std::cout<<"NODENAME "<<XMLString::transcode(node->getTextContent())<<std::endl;
406 vect.push_back(XMLString::transcode(node->getTextContent()));
412 void OsirixParser::saveCreaContoursFormat(){
413 FILE *pFile=fopen(TEMPIMPORTOSIRIXFILE.c_str(),"w+");
417 writeContours(pFile);
418 writeContoursStatic(pFile);
423 void OsirixParser::writeContoursStatic(FILE* pFile){
424 fprintf(pFile, CREACONTOUR_IMAGEDIMENSIONS);
425 fprintf(pFile, " %d %d %d\n", _extent[1] - _extent[0],_extent[3] - _extent[2], _extent[5] - _extent[4]);
426 fprintf(pFile, CREACONTOUR_IMAGESPACING);
427 fprintf(pFile, " %f %f %f\n", _spacing[0], _spacing[1], _spacing[2]);
428 fprintf(pFile, CREACONTOUR_NUMBEROFCONTOURSSTATIC);
429 fprintf(pFile, " 0\n");
432 void OsirixParser::writeContours(FILE* pFile){
434 map<int, vectorXYZ>::iterator itPX;
435 vector<double> vectx, vecty, vectz;
437 int dimz = 0, dimy = 0;
440 dimz = _extent[5] - _extent[4] + 1;
441 dimy = _extent[3] - _extent[2] + 1;
444 /*for (itMM = contoursmapMM.begin(), itPX = contoursmapPX.begin();
445 itMM != contoursmapMM.end(), itPX != contoursmapPX.end();
447 for (itPX = contoursmapPX.begin(); itPX != contoursmapPX.end(); itPX++ ){
449 vectx = ((*itPX).second)[0];
450 vecty = ((*itPX).second)[1];
451 vectz = ((*itPX).second)[2];
455 valuez = (int) vectz[0] / _spacing[2];
457 fprintf(pFile, CREACONTOUR_INSTANT);
458 fprintf(pFile, " 1 %d 1 1 1 1\n",dimz - valuez);
459 fprintf(pFile, CREACONTOUR_TYPEMODEL);
460 fprintf(pFile, " 1\n");
461 fprintf(pFile, CREACONTOUR_NUMBEROFCONTROLPOINTS);
462 fprintf(pFile, " %d\n", vectz.size());
463 for(i = 0; i < vectx.size(); i++){
464 fprintf(pFile, "%f %f 900.00\n", vectx[i]/ _spacing[0], dimy - vecty[i]/ _spacing[1]);
465 // fprintf(pFile, "%f %f %f\n", vectx[i] , vecty[i], vectz[i]);
467 fprintf(pFile, CREACONTOUR_TYPEVIEW);
468 fprintf(pFile, " 1\n");
474 void OsirixParser::writeHeader(FILE* pFile){
477 fprintf(pFile, CREACONTOUR);
478 fprintf(pFile, "\n");
479 fprintf(pFile, CREACONTOUR_VERSION);
480 fprintf(pFile, "\n");
481 fprintf(pFile, CREACONTOUR_IMAGEDIMENSIONS);
482 fprintf(pFile, " %d %d %d\n", _extent[1] - _extent[0],_extent[3] - _extent[2], _extent[5] - _extent[4]);
483 fprintf(pFile, CREACONTOUR_IMAGESPACING);
484 fprintf(pFile, " %f %f %f\n", _spacing[0], _spacing[1], _spacing[2]);
485 fprintf(pFile, CREACONTOUR_NUMBEROFCONTOURS);
486 fprintf(pFile, " %d\n", contoursmapPX.size());
493 std::string OsirixParser::getContoursFileName(){
494 return TEMPIMPORTOSIRIXFILE;
498 ** The Error Handler's interface implementation
502 ** Default Constructor
504 OsirixParserErrorHandler::OsirixParserErrorHandler()
512 ** Desctructor by defect
514 OsirixParserErrorHandler::~OsirixParserErrorHandler(){
517 void OsirixParserErrorHandler::warning (const SAXParseException &exc){
520 void OsirixParserErrorHandler::error (const SAXParseException &exc){
522 errormsg = "Column ";
523 // errormsg += itoa(exc.getColumnNumber(),c,10);
525 sprintf_s(c,"%d",(int)(exc.getColumnNumber()));
527 sprintf(c,"%d",(int)(exc.getColumnNumber()));
529 errormsg += std::string(c);
531 errormsg += " Line ";
532 // errormsg += itoa(exc.getLineNumber(),c,10);
534 sprintf_s(c,"%d",(int)(exc.getLineNumber()));
536 sprintf(c,"%d",(int)(exc.getLineNumber()));
538 errormsg += std::string(c);
541 errormsg += XMLString::transcode(exc.getMessage());
545 void OsirixParserErrorHandler::fatalError (const SAXParseException &exc){
547 errormsg = "Column ";
548 // errormsg += itoa(exc.getColumnNumber(),c,10);
550 sprintf_s(c,"%d",(int)(exc.getColumnNumber()));
552 sprintf(c,"%d",(int)(exc.getColumnNumber()));
554 errormsg += std::string(c);
556 errormsg += " Line ";
557 // errormsg += itoa(exc.getLineNumber(),c,10);
559 sprintf_s(c,"%d",(int)(exc.getLineNumber()));
561 sprintf(c,"%d",(int)(exc.getLineNumber()));
563 errormsg += std::string(c);
566 errormsg += XMLString::transcode(exc.getMessage());
569 void OsirixParserErrorHandler::resetErrors (){
572 std::string OsirixParserErrorHandler::getErrorMsg(){
573 return this->errormsg;