/tags/xplanet-1.0.2/src/libannotate/addArcs.cpp

# · C++ · 265 lines · 239 code · 26 blank · 0 comment · 44 complexity · 7c86b1107ea74d2bd339e6faea0abbdb MD5 · raw file

  1. #include <cstdio>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <map>
  5. #include <string>
  6. #include <vector>
  7. using namespace std;
  8. #include "findFile.h"
  9. #include "keywords.h"
  10. #include "Options.h"
  11. #include "parse.h"
  12. #include "PlanetProperties.h"
  13. #include "sphericalToPixel.h"
  14. #include "xpUtil.h"
  15. #include "drawArc.h"
  16. #include "libannotate/libannotate.h"
  17. #include "libplanet/Planet.h"
  18. #include "libprojection/ProjectionBase.h"
  19. class View;
  20. static void
  21. readArcFile(const char *line, Planet *planet,
  22. View *view, ProjectionBase *projection,
  23. unsigned char *color,
  24. const double magnify,
  25. multimap<double, Annotation *> &annotationMap)
  26. {
  27. int i = 0, j = 0, k = 0;
  28. while (isDelimiter(line[i]))
  29. {
  30. i++;
  31. if (static_cast<unsigned int> (i) > strlen(line)) return;
  32. }
  33. if (isEndOfLine(line[i])) return;
  34. Options *options = Options::getInstance();
  35. double coords[4];
  36. double radius[2] = { 1, 1 };
  37. double spacing = 0.1;
  38. bool syntaxError = false;
  39. while (static_cast<unsigned int> (i) < strlen(line))
  40. {
  41. char *returnString = NULL;
  42. int val = parse(i, line, returnString);
  43. switch (val)
  44. {
  45. case COLOR:
  46. {
  47. int r, g, b;
  48. if (sscanf(returnString, "%d,%d,%d", &r, &g, &b) == 3)
  49. {
  50. color[0] = r & 0xff;
  51. color[1] = g & 0xff;
  52. color[2] = b & 0xff;
  53. }
  54. else
  55. {
  56. xpWarn("Need three values for color\n",
  57. __FILE__, __LINE__);
  58. syntaxError = true;
  59. }
  60. }
  61. break;
  62. case LATLON:
  63. if (j < 4)
  64. {
  65. sscanf(returnString, "%lf", &coords[j]);
  66. if (j%2 == 0)
  67. {
  68. if (coords[j] < -90 || coords[j] > 90)
  69. {
  70. ostringstream errMsg;
  71. errMsg << "Latitude value must be between -90 "
  72. << "and 90 degrees\n";
  73. xpWarn(errMsg.str(), __FILE__, __LINE__);
  74. syntaxError = true;
  75. }
  76. }
  77. else
  78. {
  79. if (coords[j] < -360 || coords[j] > 360)
  80. {
  81. ostringstream errMsg;
  82. errMsg << "Longitude value must be between -360 "
  83. << "and 360 degrees\n";
  84. xpWarn(errMsg.str(), __FILE__, __LINE__);
  85. syntaxError = true;
  86. }
  87. }
  88. coords[j] *= deg_to_rad;
  89. j++;
  90. }
  91. else
  92. {
  93. syntaxError = true;
  94. }
  95. break;
  96. case RADIUS:
  97. if (k < 2)
  98. {
  99. sscanf(returnString, "%lf", &radius[k]);
  100. if (radius[k] < 0)
  101. {
  102. xpWarn("Radius value must be positive\n",
  103. __FILE__, __LINE__);
  104. radius[k] = 1;
  105. syntaxError = true;
  106. }
  107. else
  108. k++;
  109. }
  110. break;
  111. case SPACING:
  112. sscanf(returnString, "%lf", &spacing);
  113. if (spacing < 0)
  114. {
  115. xpWarn("spacing must be positive\n",
  116. __FILE__, __LINE__);
  117. spacing = 0.1;
  118. syntaxError = true;
  119. }
  120. break;
  121. case UNKNOWN:
  122. syntaxError = true;
  123. default:
  124. case DELIMITER:
  125. break;
  126. }
  127. if (val != DELIMITER && options->Verbosity() > 3)
  128. {
  129. stringstream msg;
  130. msg << "value is " << keyWordString[val - '?'];
  131. if (returnString != NULL)
  132. msg << ", returnString is " << returnString;
  133. msg << endl;
  134. xpMsg(msg.str(), __FILE__, __LINE__);
  135. }
  136. delete [] returnString;
  137. if (syntaxError)
  138. {
  139. stringstream errStr;
  140. errStr << "Syntax error in arc file\n"
  141. << "line is \"" << line << "\"\n";
  142. xpWarn(errStr.str(), __FILE__, __LINE__);
  143. return;
  144. }
  145. if (val == ENDOFLINE) break;
  146. }
  147. if (j != 4)
  148. {
  149. stringstream errStr;
  150. errStr << "Incomplete entry in arc file\n"
  151. << "line is \"" << line << "\"\n";
  152. xpWarn(errStr.str(), __FILE__, __LINE__);
  153. return;
  154. }
  155. if (k == 0) radius[1] = radius[0];
  156. if (planet == NULL)
  157. {
  158. double X1, Y1, Z1;
  159. double X2, Y2, Z2;
  160. sphericalToPixel(coords[0], coords[1], 1.0,
  161. X1, Y1, Z1, NULL, view, NULL);
  162. sphericalToPixel(coords[2], coords[3], 1.0,
  163. X2, Y2, Z2, NULL, view, NULL);
  164. LineSegment *ls = new LineSegment(color, X2, Y2, X1, Y1);
  165. double avgZ = 0.5 * (Z1 + Z2);
  166. if (Z1 > 0 && Z2 > 0)
  167. annotationMap.insert(pair<const double, Annotation*>(avgZ, ls));
  168. }
  169. else
  170. {
  171. drawArc(coords[0], coords[1], radius[0], coords[2], coords[3],
  172. radius[1], color, spacing * deg_to_rad,
  173. magnify, planet, view, projection, annotationMap);
  174. }
  175. }
  176. void
  177. addArcs(PlanetProperties *planetProperties, Planet *planet,
  178. View *view, ProjectionBase *projection,
  179. multimap<double, Annotation *> &annotationMap)
  180. {
  181. vector<string> arcfiles = planetProperties->ArcFiles();
  182. vector<string>::iterator ii = arcfiles.begin();
  183. unsigned char color[3];
  184. memcpy(color, planetProperties->ArcColor(), 3);
  185. while (ii != arcfiles.end())
  186. {
  187. string arcFile(*ii);
  188. bool foundFile = findFile(arcFile, "arcs");
  189. if (foundFile)
  190. {
  191. ifstream inFile(arcFile.c_str());
  192. char *line = new char[256];
  193. while (inFile.getline (line, 256, '\n') != NULL)
  194. readArcFile(line, planet, view, projection,
  195. color, planetProperties->Magnify(),
  196. annotationMap);
  197. inFile.close();
  198. delete [] line;
  199. }
  200. else
  201. {
  202. stringstream errStr;
  203. errStr << "Can't load arc file " << arcFile << endl;
  204. xpWarn(errStr.str(), __FILE__, __LINE__);
  205. }
  206. ii++;
  207. }
  208. }
  209. void
  210. addArcs(View *view, multimap<double, Annotation *> &annotationMap)
  211. {
  212. Options *options = Options::getInstance();
  213. vector<string> arcfiles = options->ArcFiles();
  214. vector<string>::iterator ii = arcfiles.begin();
  215. unsigned char color[3];
  216. memset(color, 128, 3);
  217. while (ii != arcfiles.end())
  218. {
  219. string arcFile(*ii);
  220. bool foundFile = findFile(arcFile, "arcs");
  221. if (foundFile)
  222. {
  223. ifstream inFile(arcFile.c_str());
  224. char *line = new char[256];
  225. while (inFile.getline (line, 256, '\n') != NULL)
  226. readArcFile(line, NULL, view, NULL,
  227. color, 1.0, annotationMap);
  228. inFile.close();
  229. delete [] line;
  230. }
  231. else
  232. {
  233. stringstream errStr;
  234. errStr << "Can't load arc file " << arcFile << endl;
  235. xpWarn(errStr.str(), __FILE__, __LINE__);
  236. }
  237. ii++;
  238. }
  239. }