home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / PASM.LZH / DESIGN.CPP < prev    next >
C/C++ Source or Header  |  1996-07-29  |  31KB  |  1,197 lines

  1. #include <owl\owlpch.h>
  2. #pragma hdrstop
  3.  
  4. #include <dir.h>
  5. #include <owl\scrollba.h>
  6. #include <owl\statusba.h>
  7. #include <owl\buttonga.h>
  8. #include <owl\controlg.h>
  9. #include <owl\button.h>
  10. #include "matrix.h"
  11. #include "suflib.h"
  12. #include "design.h"
  13. #include "display.h"
  14. #include "status.h"
  15. #include "pers.h"
  16. #include "winframe.h"
  17. #include "parts.h"
  18. #include "files.h"
  19. #include "syscolor.h"
  20. #include "polyline.h"
  21. #include "log.h"
  22.  
  23. #if 0
  24. static const TColor& cWireSelect = TColor::LtBlue;
  25. static const TColor& cWireNoSelect = TColor::LtGreen;
  26.  
  27. static const TColor& cMarker = TColor::LtRed;
  28. static const TColor& cEye    = TColor::LtCyan;
  29. static const TColor& cCamera = TColor::LtYellow;
  30. #endif
  31.  
  32. const double scalemax = 4.0;
  33. const double scalemin = (1/32.0);
  34. const double scalerate = 1.414213562373;
  35. static const int EyeLength = 500;
  36. //const int scrollunit = 50;
  37.  
  38.  
  39. DesignData::DesignData()
  40. {
  41. //    for (int i = 0; i < MaxSufFile; ++i) {
  42. //        suffile[i] = NULL;
  43. //    }
  44.     displayscale = 0.125;
  45.     displayoffset = Vector(0.0, 0.0, 0.0);
  46.     zoommode = ZoomNone;
  47.  
  48.     wireframe.line = NULL;
  49.     hiddenremove = FALSE;
  50.     meshflag = TRUE;
  51.     meshspacing = 200;
  52.  
  53.     gridflag = TRUE;
  54.     gridspacing = 50;
  55.     rotspacing = 10;
  56.     scalespacing = (int)(0.1 * scalespacingfactor);
  57.  
  58.     getcwd(filedir, filenamedirlength);
  59.     filename[0] = '\0';
  60.     cataloglastmenu = 0;
  61.     cataloglastparts = -1;
  62.  
  63.     polygons = 0;
  64. //    SetPolygons(0);
  65.  
  66.     dragtype = DragCompound;
  67.     parts = select = NULL;
  68.     selectstatus = SelNone;
  69.     camera = new CameraParts(Vector(1200,-900,900),Vector(0,0,0));
  70.     combined = new CombinedParts();
  71.     editflag = 0;
  72.  
  73.     color = ColorWhite;
  74.     if (rendcolor == "青") color = ColorBlue;
  75.     if (rendcolor == "赤") color = ColorRed;
  76.     if (rendcolor == "緑") color = ColorGreen;
  77.     if (rendcolor == "紫") color = ColorMagenta;
  78.  
  79.     if (rendlight == "左上") Parts::SetLightType(LightLeftUpper);
  80.     if (rendlight == "左下") Parts::SetLightType(LightLeftLower);
  81.     if (rendlight == "右上") Parts::SetLightType(LightRightUpper);
  82.     if (rendlight == "右下") Parts::SetLightType(LightRightLower);
  83.  
  84. //    parts = camera;
  85. }
  86.  
  87. #if 0
  88. void DesignData::ShowCube(Matrix& m, int flag)
  89. {
  90.     if (select == camera) {
  91.         TopView->ShowCamera(m, flag);
  92.         SideView->ShowCamera(m, flag);
  93.         FrontView->ShowCamera(m, flag);
  94.     } else {
  95.         SelectType t[3];
  96.         SelectAxis(t, m);
  97.         TopView->ShowCube(m, flag, t[0]);
  98.         SideView->ShowCube(m, flag, t[1]);
  99.         FrontView->ShowCube(m, flag, t[2]);
  100.     }
  101. }
  102. #endif
  103.  
  104. void DesignData::ShowCube(Matrix& omat, Matrix& nmat)
  105. {
  106.     if (select == camera) {
  107.         TopView->ShowCamera(omat, FALSE);
  108.         TopView->ShowCamera(nmat, FALSE);
  109.         SideView->ShowCamera(omat, FALSE);
  110.         SideView->ShowCamera(nmat, FALSE);
  111.         FrontView->ShowCamera(omat, FALSE);
  112.         FrontView->ShowCamera(nmat, FALSE);
  113.     } else {
  114.         TopView->ShowCube(omat, FALSE, SelNone);
  115.         TopView->ShowCube(nmat, FALSE, SelNone);
  116.         SideView->ShowCube(omat, FALSE, SelNone);
  117.         SideView->ShowCube(nmat, FALSE, SelNone);
  118.         FrontView->ShowCube(omat, FALSE, SelNone);
  119.         FrontView->ShowCube(nmat, FALSE, SelNone);
  120.     }
  121. }
  122.  
  123. void DesignData::CalcView(Parts *p)
  124. {
  125.     int fx, fy;
  126.  
  127.     fx = PersView->Attr.W;
  128.     fy = PersView->Attr.H;
  129.  
  130.     Matrix cmat;
  131.     cmat = camera->GetMatrix().inv();
  132.  
  133.     Matrix tmpmat(0);
  134.     tmpmat.v[0].z = tmpmat.v[1].x = tmpmat.v[2].y = 1.0;
  135.     cmat = tmpmat * cmat;
  136.  
  137.     tmpmat = Matrix(1);
  138.     tmpmat.v[2].x = double(fx)/2.0;
  139.     tmpmat.v[2].y = double(fy)/2.0;
  140.     tmpmat.v[0].x = - fx / 2.0 / tan(camera->angle/2.0);
  141.     tmpmat.v[1].y = tmpmat.v[0].x;
  142.     cmat = tmpmat * cmat;
  143.  
  144.     p->CalcView(cmat * p->GetMatrix());
  145. }
  146.  
  147. void DesignData::CalcViewAll(void)
  148. {
  149.     for (Parts *p = parts; p != NULL; p = p->next) {
  150.         CalcView(p);
  151.     }
  152.     BuildWireFrame();
  153. }
  154.  
  155. void DesignData::CalcPoints(Parts *p)
  156. {
  157.     p->CalcPoints(GetMatrix(p));
  158.     CalcView(p);
  159.     delete [] wireframe.line;
  160.     wireframe.line = NULL;
  161. }
  162.  
  163. void DesignData::ShowObject(Parts *p, SelectType type, int flag)
  164. {
  165.     int centerx, centery, centerz;
  166.  
  167.     centerx = TopView->Attr.W / 2;
  168.     centery = TopView->Attr.H / 2;
  169.     centerz = SideView->Attr.H / 2;
  170.  
  171.     Matrix m = GetMatrix(p);
  172.  
  173.     if (flag == TRUE) {
  174.         CalcPoints(p);
  175.     }
  176.  
  177.     if (type & SelX) {
  178.         TClientDC dc(*FrontView);
  179.  
  180.         int i = 0;
  181.         int begin, end = -1;
  182.         int cx, cy;
  183.         cx = centery;// + m.v[3].y;
  184.         cy = centerz;// - m.v[3].z;
  185.  
  186.         if (p == select) {
  187.             dc.SelectObject(TPen(cWireSelect));
  188.         } else if (p == camera) {
  189.             if (select != NULL) {
  190.                 dc.SelectObject(TPen(cWireCameraNoSelect));
  191.             } else {
  192.                 dc.SelectObject(TPen(cWireCamera));
  193.             }
  194. #if 0
  195.          && select == NULL) {
  196.             i = 1;
  197.             dc.SelectObject(TPen(cEye));
  198.             begin = p->line_1[0];
  199.             end = p->line_2[0];
  200.             dc.MoveTo(cx + p->point_y[begin], cy - p->point_z[begin]);
  201.             dc.LineTo(cx + p->point_y[end],   cy - p->point_z[end]);
  202.             dc.SelectObject(TPen(cCamera));
  203. #endif
  204.         } else {
  205.             if (select != NULL) {
  206.                 dc.SelectObject(TPen(cWireNoSelect));
  207.             } else {
  208.                 dc.SelectObject(TPen(cWireNormal));
  209.             }
  210.         }
  211.  
  212.         for (; i < p->lines; ++i) {
  213.             if ((begin = p->line_1[i]) != end) {
  214.                 dc.MoveTo(cx + p->point_y[begin], cy - p->point_z[begin]);
  215.             }
  216.             end = p->line_2[i];
  217.             dc.LineTo(cx + p->point_y[end],   cy - p->point_z[end]);
  218.         }
  219.  
  220.         if (select == NULL) {
  221.             dc.SelectObject(TPen(cMarker));
  222.             dc.SelectObject(TBrush(cMarker));
  223.             dc.Rectangle(cx+m.v[3].y-markersize, cy-m.v[3].z-markersize,
  224.                          cx+m.v[3].y+markersize, cy-m.v[3].z+markersize);
  225.         }
  226.     }
  227.     if (type & SelY) {
  228.         TClientDC dc(*SideView);
  229.  
  230.         int i = 0;
  231.         int begin, end = -1;
  232.         int cx, cy;
  233.         cx = centerx;// + m.v[3].x;
  234.         cy = centerz;// - m.v[3].z;
  235.         if (p == select) {
  236.             dc.SelectObject(TPen(cWireSelect));
  237.         } else if (p == camera) {
  238.             if (select != NULL) {
  239.                 dc.SelectObject(TPen(cWireCameraNoSelect));
  240.             } else {
  241.                 dc.SelectObject(TPen(cWireCamera));
  242.             }
  243. #if 0
  244.          && select == NULL) {
  245.             i = 1;
  246.             dc.SelectObject(TPen(cEye));
  247.             begin = p->line_1[0];
  248.             end = p->line_2[0];
  249.             dc.MoveTo(cx + p->point_y[begin], cy - p->point_z[begin]);
  250.             dc.LineTo(cx + p->point_y[end],   cy - p->point_z[end]);
  251.             dc.SelectObject(TPen(cCamera));
  252. #endif
  253.         } else {
  254.             if (select != NULL) {
  255.                 dc.SelectObject(TPen(cWireNoSelect));
  256.             } else {
  257.                 dc.SelectObject(TPen(cWireNormal));
  258.             }
  259.         }
  260.  
  261.         for (; i < p->lines; ++i) {
  262.             if ((begin = p->line_1[i]) != end) {
  263.                 dc.MoveTo(cx + p->point_x[begin], cy - p->point_z[begin]);
  264.             }
  265.             end = p->line_2[i];
  266.             dc.LineTo(cx + p->point_x[end],   cy - p->point_z[end]);
  267.         }
  268.  
  269.         if (select == NULL) {
  270.             dc.SelectObject(TPen(cMarker));
  271.             dc.SelectObject(TBrush(cMarker));
  272.             dc.Rectangle(cx+m.v[3].x-markersize, cy-m.v[3].z-markersize,
  273.                          cx+m.v[3].x+markersize, cy-m.v[3].z+markersize);
  274.         }
  275.     }
  276.     if (type & SelZ) {
  277.         TClientDC dc(*TopView);
  278.  
  279.         int i = 0;
  280.         int begin, end = -1;
  281.         int cx, cy;
  282.         cx = centerx;// + m.v[3].x;
  283.         cy = centery;// - m.v[3].y;
  284.  
  285.         if (p == select) {
  286.             dc.SelectObject(TPen(cWireSelect));
  287.         } else if (p == camera) {
  288.             if (select != NULL) {
  289.                 dc.SelectObject(TPen(cWireCameraNoSelect));
  290.             } else {
  291.                 dc.SelectObject(TPen(cWireCamera));
  292.             }
  293. #if 0
  294.          && select == NULL) {
  295.             i = 1;
  296.             dc.SelectObject(TPen(cEye));
  297.             begin = p->line_1[0];
  298.             end = p->line_2[0];
  299.             dc.MoveTo(cx + p->point_y[begin], cy - p->point_z[begin]);
  300.             dc.LineTo(cx + p->point_y[end],   cy - p->point_z[end]);
  301.             dc.SelectObject(TPen(cCamera));
  302. #endif
  303.         } else {
  304.             if (select != NULL) {
  305.                 dc.SelectObject(TPen(cWireNoSelect));
  306.             } else {
  307.                 dc.SelectObject(TPen(cWireNormal));
  308.             }
  309.         }
  310.  
  311.         for (; i < p->lines; ++i) {
  312.             if ((begin = p->line_1[i]) != end) {
  313.                 dc.MoveTo(cx + p->point_x[begin], cy - p->point_y[begin]);
  314.             }
  315.             end = p->line_2[i];
  316.             dc.LineTo(cx + p->point_x[end],   cy - p->point_y[end]);
  317.         }
  318.  
  319.         if (select == NULL) {
  320.             dc.SelectObject(TPen(cMarker));
  321.             dc.SelectObject(TBrush(cMarker));
  322.             dc.Rectangle(cx+m.v[3].x-markersize, cy-m.v[3].y-markersize,
  323.                          cx+m.v[3].x+markersize, cy-m.v[3].y+markersize);
  324.         }
  325.     }
  326.     if (type & SelPers) {
  327.         TClientDC dc(*PersView);
  328.  
  329.         if (p == select) {
  330.             dc.SelectObject(TPen(cWireSelect));
  331.         } else if (p == camera) {
  332.             dc.SelectObject(TPen(cWireCamera));
  333.         } else if (select != NULL && select != camera) {
  334.             dc.SelectObject(TPen(cWireNoSelect));
  335.         } else {
  336.             dc.SelectObject(TPen(cWireNormal));
  337.         }
  338.         int begin, end = -1;
  339.         for (int i = 0; i < p->lines; ++i) {
  340.             if (p->view_z[p->line_1[i]] <= 0 || p->view_z[p->line_2[i]] <= 0) {
  341.                 end = -1;
  342.             } else {
  343.                 if ((begin = p->line_1[i]) != end) {
  344.                     dc.MoveTo(p->view_x[begin], p->view_y[begin]);
  345.                 }
  346.                 end = p->line_2[i];
  347.                 dc.LineTo(p->view_x[end], p->view_y[end]);
  348.             }
  349.         }
  350.     }
  351. }
  352.  
  353. Matrix DesignData::GetMatrix(Parts *p)
  354. {
  355.     Matrix m;
  356. //    m = Matrix(1).move(displayoffset).scale(displayscale) * p->GetMatrix(mat);
  357.     m = Matrix::m_move(displayoffset).scale(displayscale) * p->GetMatrix();
  358.     return m;
  359.  
  360. }
  361.  
  362. void DesignData::GetCubeMatrix(Parts*p, Matrix& move, Matrix& rot, Matrix& scal)
  363. {
  364.     double maxx, maxy, maxz;
  365.     const int minimumsize = markersize * 8;
  366.     if (p == camera) {
  367. #if 0
  368.         maxx = (camera->target - camera->position).length();
  369.         maxy = int(double(maxx) * tan(camera->angle/2.0));
  370.         maxz = maxy * 3 / 4;
  371. #endif
  372.         maxx = EyeLength;
  373.         maxy = maxx * tan(camera->angle/2.0);
  374.         maxz = maxy * 3 / 4;
  375.     } else {
  376.  
  377.         if (p->maxx > -p->minx) {
  378.             maxx = p->maxx;
  379.         } else {
  380.             maxx = -p->minx;
  381.         }
  382.         if (p->maxy > -p->miny) {
  383.             maxy = p->maxy;
  384.         } else {
  385.             maxy = -p->miny;
  386.         }
  387.         if (p->maxz > -p->minz) {
  388.             maxz = p->maxz;
  389.         } else {
  390.             maxz = -p->minz;
  391.         }
  392.     }
  393.     p->GetMatrix(move, rot, scal);
  394.     move = Matrix(1).move(displayoffset).scale(displayscale) * move;
  395.     scal = scal.scale(maxx, maxy, maxz);
  396.     if (p != camera) {
  397.         if (scal.v[0].x > 0.0) {
  398.             if (scal.v[0].x <  minimumsize/displayscale) scal.v[0].x =  minimumsize/displayscale;
  399.         } else {
  400.             if (scal.v[0].x > -minimumsize/displayscale) scal.v[0].x = -minimumsize/displayscale;
  401.         }
  402.         if (scal.v[1].y > 0.0) {
  403.             if (scal.v[1].y <  minimumsize/displayscale) scal.v[1].y =  minimumsize/displayscale;
  404.         } else {
  405.             if (scal.v[1].y > -minimumsize/displayscale) scal.v[1].y = -minimumsize/displayscale;
  406.         }
  407.         if (scal.v[2].z > 0.0) {
  408.             if (scal.v[2].z <  minimumsize/displayscale) scal.v[2].z =  minimumsize/displayscale;
  409.         } else {
  410.             if (scal.v[2].z > -minimumsize/displayscale) scal.v[2].z = -minimumsize/displayscale;
  411.         }
  412.     }
  413. }
  414.  
  415. Matrix DesignData::GetCubeMatrix(Parts *p)
  416. {
  417.     Matrix m,r,s;
  418.     GetCubeMatrix(p, m, r, s);
  419.     return m * r * s;
  420. }
  421.  
  422.  
  423. void DesignData::PartsAdd(Object *obj, Vector& m, Vector& r, Vector& s)
  424. {
  425.     Parts *p = new Parts(obj, m, r, s);
  426.     p->next = parts;
  427.  
  428.     SetPolygons(polygons + obj->polygons);
  429.  
  430.     parts = p;
  431.     SelectParts(p);
  432.     CalcPoints(p);
  433.     BuildWireFrame();
  434.     Redraw();
  435.     editflag++;
  436. }
  437.  
  438. void DesignData::PartsDelete(Parts *p)
  439. {
  440.     if (p == combined) {
  441.         Parts * nparts = NULL;
  442.         Parts *next;
  443.         combined->DeleteAllParts();
  444.         for (Parts *pt = parts; pt != NULL; pt = next) {
  445.             next = pt->next;
  446.             if (pt->sel) {
  447.                 polygons -= pt->polygons;
  448.                 delete pt;
  449.             } else {
  450.                 pt->next = nparts;
  451.                 nparts = pt;
  452.             }
  453.         }
  454.         parts = nparts;
  455.         SetPolygons(polygons);
  456.     } else if (p != NULL && p != camera) {
  457.         for (Parts *pt = parts; pt != NULL; pt = pt->next) {
  458.             if (pt->next == p) {
  459.                 pt->next = p->next;
  460.                 break;
  461.             }
  462.         }
  463.         SetPolygons(polygons - p->polygons);
  464.         if (parts == p) {
  465.             parts = p->next;
  466.         }
  467.         delete p;
  468.     }
  469.     SelectParts(NULL);
  470.     delete [] wireframe.line;
  471.     wireframe.line = NULL;
  472.     Redraw();
  473.  
  474.     editflag++;
  475. }
  476.  
  477. void DesignData::SetScrollLimit(int flag)
  478. {
  479.     int WX = TopView->Attr.W;
  480.     int WY = TopView->Attr.H;
  481.     int WZ = SideView->Attr.H;
  482.  
  483.     int lx = limitdim/scrollstep - (WX/2) / displayscale / scrollstep;
  484.     int ly = limitdim/scrollstep - (WY/2) / displayscale / scrollstep;
  485.     int lz = limitdim/scrollstep - (WZ/2) / displayscale / scrollstep;
  486.     scrollX->SetRange(-lx, lx);
  487.     scrollY->SetRange(-ly, ly);
  488.     scrollZ->SetRange(-lz, lz);
  489.  
  490.     scrollX->PageMagnitude = WX / displayscale / scrollstep;
  491.     scrollX->LineMagnitude = (scrollX->PageMagnitude+7) / 8;
  492.     scrollY->PageMagnitude = WY / displayscale / scrollstep;
  493.     scrollY->LineMagnitude = (scrollY->PageMagnitude+7) / 8;
  494.     scrollZ->PageMagnitude = WZ / displayscale / scrollstep;
  495.     scrollZ->LineMagnitude = (scrollZ->PageMagnitude+7) / 8;
  496.  
  497.     if (flag) {
  498.         scrollX->SetPosition(-displayoffset.x / displayscale / scrollstep);
  499.         scrollY->SetPosition(displayoffset.y / displayscale / scrollstep);
  500.         scrollZ->SetPosition(displayoffset.z / displayscale / scrollstep);
  501.     }
  502.  
  503.     if (-displayoffset.x / displayscale / scrollstep < -lx) {
  504.         displayoffset.x =  lx * displayscale * scrollstep;
  505.         scrollX->SetPosition(-lx);
  506.     } else if (-displayoffset.x / displayscale / scrollstep> lx) {
  507.         displayoffset.x = -lx * displayscale * scrollstep;
  508.         scrollX->SetPosition(lx);
  509.     }
  510.     if (displayoffset.y / displayscale / scrollstep < -ly) {
  511.         displayoffset.y = -ly * displayscale * scrollstep;
  512.         scrollX->SetPosition(-ly);
  513.     } else if (displayoffset.y / displayscale / scrollstep> ly) {
  514.         displayoffset.y =  ly * displayscale * scrollstep;
  515.         scrollX->SetPosition(ly);
  516.     }
  517.     if (displayoffset.z / displayscale / scrollstep< -lz) {
  518.         displayoffset.z = -lz * displayscale * scrollstep;
  519.         scrollX->SetPosition(-lz);
  520.     } else if (displayoffset.z / displayscale / scrollstep> lz) {
  521.         displayoffset.z =  lz * displayscale * scrollstep;
  522.         scrollX->SetPosition(lz);
  523.     }
  524. //    if (zoommode == ZoomNone) {
  525.         scrollX->Show(SW_HIDE); scrollX->Show(SW_SHOW);
  526.         scrollY->Show(SW_HIDE); scrollY->Show(SW_SHOW);
  527.         scrollZ->Show(SW_HIDE); scrollZ->Show(SW_SHOW);
  528. //    }
  529.  
  530. }
  531.  
  532. void DesignData::ScaleUp(void)
  533. {
  534.     if (displayscale * scalerate <= scalemax) {
  535.         displayscale *= scalerate;
  536.         displayoffset *= scalerate;
  537.  
  538.         SetScrollLimit();
  539.  
  540.         Redraw(SelXYZ, TRUE);
  541.     }
  542. }
  543.  
  544. void DesignData::ScaleDown(void)
  545. {
  546.     if (displayscale / scalerate >= scalemin) {
  547.         displayscale /= scalerate;
  548.         displayoffset *= (1.0/scalerate);
  549.  
  550.         SetScrollLimit();
  551.  
  552.         Redraw(SelXYZ, TRUE);
  553.     }
  554. }
  555.  
  556. int DesignData::ScaleUpLimitCheck(void)
  557. {
  558.     return zoommode == ZoomNone && (displayscale <= scalemax/scalerate);
  559. }
  560.  
  561. int DesignData::ScaleDownLimitCheck(void)
  562. {
  563.     return zoommode == ZoomNone && (displayscale >= scalemin*scalerate);
  564. }
  565.  
  566. void DesignData::PartsCopy(Parts *p, SelectType t)
  567. {
  568.     if (p != NULL && p != camera) {
  569.         Parts *np;
  570.         if (p == combined) {
  571.             combined->DeleteAllParts();
  572.             np = NULL;
  573.             Parts *tp, *ntp, *lp;
  574.             for (tp = parts; tp != NULL; tp = tp->next) {
  575.                 lp = tp;
  576.                 if (tp->sel) {
  577.                     ntp = new Parts(tp);
  578.                     ntp->next = np;
  579.                     np = ntp;
  580.                     np->Trans(t);
  581.                     np->sel = TRUE;
  582.                     polygons += np->polygons;
  583.                     combined->AddParts(np);
  584.                     tp->sel = FALSE;
  585.                 }
  586.             }
  587.             lp->next = np;
  588.             combined->CalcSizeAll();
  589.             np = combined;
  590.             SetPolygons(polygons);
  591.         } else {
  592.             np = new Parts(p);
  593.             np->next = parts;
  594.             parts = np;
  595.             np->Trans(t);
  596.             SetPolygons(polygons + p->polygons);
  597.         }
  598.         Vector wh(TopView->Attr.W, TopView->Attr.H, SideView->Attr.H);
  599.         if (t == SelNone) {
  600. #if 0
  601.             np->position += wh * (1.0/displayscale/8.0);
  602.             if (np->position.x > limitdim) {
  603.                 np->position.x -= wh.x * (2.0 * 1.0/displayscale/8.0);
  604.             }
  605.             if (np->position.y > limitdim) {
  606.                 np->position.y -= wh.y * (2.0 * 1.0/displayscale/8.0);
  607.             }
  608.             if (np->position.z > limitdim) {
  609.                 np->position.z -= wh.z * (2.0 * 1.0/displayscale/8.0);
  610.             }
  611. #else
  612.             np->position += Vector(wh.x, wh.y, 0.0) * (1.0/displayscale/8.0);
  613.             if (np->position.x > limitdim) {
  614.                 np->position.x -= wh.x * (2.0 * 1.0/displayscale/8.0);
  615.             }
  616.             if (np->position.y > limitdim) {
  617.                 np->position.y -= wh.y * (2.0 * 1.0/displayscale/8.0);
  618.             }
  619. #endif
  620.         }
  621.         Vector off = displayoffset * (-1.0 / displayscale);
  622.         wh *= (0.5 / displayscale);
  623.         SelectParts(np);
  624.         CalcPoints(np);
  625.  
  626.         if (off.x + wh.x < np->position.x + np->minx
  627.          || off.x - wh.x > np->position.x + np->maxx
  628.          || off.y + wh.y < np->position.y + np->miny
  629.          || off.y - wh.y > np->position.y + np->maxy
  630.          || off.z + wh.z < np->position.z + np->minz
  631.          || off.z - wh.z > np->position.z + np->maxz) {
  632.  
  633.             displayoffset = np->position * (-displayscale);
  634.             SetScrollLimit();
  635.  
  636.             Redraw(SelAll, TRUE);
  637.  
  638.         } else {
  639.             Redraw();
  640.         }
  641.  
  642.  
  643.         editflag++;
  644.     }
  645. }
  646.  
  647. //void DesignData::PartsAdd(SufFile* suf)
  648. //{
  649. //    if (suf->object[0] != NULL) {
  650. //        PartsAdd(suf->object[0]);
  651. //    }
  652. //}
  653.  
  654. void DesignData::PartsAdd(char *filename, Vector& m, Vector& r, Vector& s)
  655. {
  656.     Object *obj;
  657.  
  658.     char str[_MAX_PATH];
  659.     if ((obj = SufFile::OpenObject(filename)) != NULL) {
  660.         PartsAdd(obj);
  661.         return;
  662.     }
  663.     strcpy(str, simplepartsdir.c_str());
  664.     strcat(str, filename);
  665.     if ((obj = SufFile::OpenObject(str)) != NULL) {
  666.         PartsAdd(obj, m, r, s);
  667.         return;
  668.     }
  669. //    sprintf(str, "部品(%s)が読み込めません", filename);
  670. //    MessageBox(str, "部品追加", MB_OK);
  671. }
  672.  
  673. void DesignData::Redraw(SelectType t, int flag)
  674. {
  675. #if 0
  676.     TDC *dc;
  677.  
  678.     if (t & SelX) {
  679.         dc = new TClientDC(*FrontView);
  680.         dc->SelectObject(TPen(cBackGround));
  681.         dc->SelectObject(TBrush(cBackGround));
  682. //        dc->SelectStockObject(WHITE_PEN);
  683.         dc->Rectangle(dc->GetClipBox());
  684.         FrontView->ShowGrid();
  685.         dc->SetBkColor(cBackGround);
  686.         dc->SetTextColor(TColor::White);
  687.         dc->TextOut(0,0,"前");
  688.         delete(dc);
  689.     }
  690.  
  691.     if (t & SelY) {
  692.         dc = new TClientDC(*SideView);
  693.         dc->SelectObject(TPen(cBackGround));
  694.         dc->SelectObject(TBrush(cBackGround));
  695. //        dc->SelectStockObject(WHITE_PEN);
  696.         dc->Rectangle(dc->GetClipBox());
  697.         SideView->ShowGrid();
  698.         dc->SetBkColor(cBackGround);
  699.         dc->SetTextColor(TColor::White);
  700.         dc->TextOut(0,0,"横");
  701.         delete(dc);
  702.     }
  703.     if (t & SelZ) {
  704.         dc = new TClientDC(*TopView);
  705.         dc->SelectObject(TPen(cBackGround));
  706.         dc->SelectObject(TBrush(cBackGround));
  707. //        dc->SelectStockObject(WHITE_PEN);
  708.         dc->Rectangle(dc->GetClipBox());
  709.         TopView->ShowGrid();
  710.         dc->SetBkColor(cBackGround);
  711.         dc->SetTextColor(TColor::White);
  712.         dc->TextOut(0,0,"上");
  713.         delete(dc);
  714.     }
  715.     if (t & SelPers) {
  716.         dc = new TClientDC(*PersView);
  717. //        dc->SelectStockObject(WHITE_PEN);
  718.         dc->SelectObject(TPen(cBackGround));
  719.         dc->SelectObject(TBrush(cBackGround));
  720.         dc->Rectangle(dc->GetClipBox());
  721.         delete(dc);
  722.     }
  723.  
  724.     for (Parts *p = parts; p != NULL; p = p->next) {
  725.         if (p != select) {
  726.             ShowObject(p, t, flag);
  727.         }
  728.     }
  729.     if (camera != select) {
  730.         ShowObject(camera, t, flag);
  731.     }
  732.     if (select != NULL) {
  733.         if (select != camera) {
  734.             ShowObject(select, t, flag);
  735.         } else {
  736.             if (flag == TRUE) {
  737.                 CalcPoints(camera);
  738.             }
  739.         }
  740.         Matrix m = GetCubeMatrix(select);
  741.         if (select == camera) {
  742.             if (t & SelX)    FrontView->ShowCamera(m, TRUE);
  743.             if (t & SelY)     SideView->ShowCamera(m, TRUE);
  744.             if (t & SelZ)     TopView->ShowCamera(m, TRUE);
  745.         } else {
  746.             SelectType mt[3];
  747.             SelectAxis(mt, m);
  748.             if (t & SelZ)     TopView->ShowCube(m, TRUE, mt[0]);
  749.             if (t & SelY)     SideView->ShowCube(m, TRUE, mt[1]);
  750.             if (t & SelX)    FrontView->ShowCube(m, TRUE, mt[2]);
  751.         }
  752.     }
  753.     if (t & SelStat) Status->Redraw();
  754. #else
  755.  
  756.     if (flag) {
  757.         for (Parts *p = parts; p != NULL; p = p->next) {
  758.             CalcPoints(p);
  759.         }
  760.         CalcPoints(camera);
  761.     }
  762.     if (flag || wireframe.line == NULL) {
  763.         BuildWireFrame();
  764.     }
  765.     if (t & SelX) FrontView->Redraw();
  766.     if (t & SelY) SideView->Redraw();
  767.     if (t & SelZ) TopView->Redraw();
  768.     if (t & SelPers) PersView->Redraw();
  769.  
  770.     if (select != NULL) {
  771.         Matrix m = GetCubeMatrix(select);
  772.         if (select == camera) {
  773.             if (t & SelX)    FrontView->ShowCamera(m, TRUE);
  774.             if (t & SelY)     SideView->ShowCamera(m, TRUE);
  775.             if (t & SelZ)     TopView->ShowCamera(m, TRUE);
  776.         } else {
  777.             SelectType mt[3];
  778.             SelectAxis(mt, m);
  779.             if (t & SelZ)     TopView->ShowCube(m, TRUE, mt[0]);
  780.             if (t & SelY)     SideView->ShowCube(m, TRUE, mt[1]);
  781.             if (t & SelX)    FrontView->ShowCube(m, TRUE, mt[2]);
  782.         }
  783.     }
  784.  
  785.     if (t & SelStat) Status->Redraw();
  786. #endif
  787. }
  788.  
  789. static void split(char *dir, char *name, char *all)
  790. {
  791.     char *p;
  792.     strcpy(dir, all);
  793.     if ((p = strrchr(dir, '\\')) != NULL) {
  794.         *p = '\0';
  795.         strcpy(name, p+1);
  796.     } else if ((p = strrchr(dir, '/')) != NULL) {
  797.         *p = '\0';
  798.         strcpy(name, p+1);
  799.     } else {
  800.         dir[0] = '\0';
  801.         strcpy(name, all);
  802.     }
  803. }
  804.  
  805. static void merge(char *all, char *dir, char *name)
  806. {
  807.     if (dir[0] == '\0') {
  808.         strcpy(all, name);
  809.     } else if (dir[1] == ':' && dir[2] == '\0') {
  810.         strcpy(all, dir);
  811.         strcat(all, name);
  812.     } else {
  813.         strcpy(all, dir);
  814.         strcat(all, "\\");
  815.         strcat(all, name);
  816.     }
  817. }
  818.  
  819.  
  820. int DesignData::FileWrite(char *fname)
  821. {
  822.     char str[filenamedirlength];
  823.     char str1[filenamedirlength];
  824.     char str2[filenamedirlength];
  825.     if (fname != NULL) {
  826.         split(str1, str2, fname);
  827. #if 0
  828.         if (isdigit(str2[0])) {
  829.             Frame->MessageBox("数字で始まるファイル名は使えません", Frame->GetApplication()->GetName(), MB_OK);
  830.             return FALSE;
  831.         }
  832. #else
  833.         int i;
  834.         if (!isalpha(str2[0]) && str2[0] != '_') {
  835.             if (isdigit(str2[0])) {
  836.                 Frame->MessageBox("数字で始まるファイル名は使えません", Frame->GetApplication()->GetName(), MB_OK);
  837.             } else {
  838.                 Frame->MessageBox("ファイル名にはアルファベット、数字、アンダーバーしか使えません", Frame->GetApplication()->GetName(), MB_OK);
  839.             }
  840.             return FALSE;
  841.         } else {
  842.             for (i = 1; str2[i] && str2[i] != '.'; i++) {
  843.                 if (!isalpha(str2[i]) && !isdigit(str2[i]) && str2[i] != '_') {
  844.                     Frame->MessageBox("ファイル名にはアルファベット、数字、アンダーバーしか使えません", Frame->GetApplication()->GetName(), MB_OK);
  845.                     return FALSE;
  846.                 }
  847.             }
  848.         }
  849. #endif
  850.         strcpy(filedir, str1);
  851.         strcpy(filename, str2);
  852.         SetCaption();
  853.     }
  854.     merge(str, filedir, filename);
  855.     if (parts->FileWrite(camera, str, filedir) == FALSE && errormessage[0]) {
  856.         PersView->GetApplication()->GetMainWindow()->MessageBox(errormessage);
  857.     }
  858.     editflag = 0;
  859.     return FALSE;
  860. }
  861.  
  862. void DesignData::FileRead(char *fname)
  863. {
  864.     Parts *np;
  865.     Parts *next;
  866.     CameraParts *nc;
  867.     char nfiledir[_MAX_PATH], nfilename[_MAX_PATH];
  868.     split(nfiledir, nfilename, fname);
  869.     if (Parts::FileRead(np, nc, fname, filedir) != FALSE) {
  870.         strcpy(filedir, nfiledir);
  871.         strcpy(filename, nfilename);
  872.         for (Parts *p = parts; p != NULL; p = next) {
  873.             next = p->next;
  874.             delete p;
  875.         }
  876.         delete camera;
  877.         select = NULL;
  878.         parts = np;
  879.         camera = nc;
  880.         polygons = 0;
  881.         for (p = parts; p != NULL; p = p->next) {
  882.             polygons += p->polygons;
  883.         }
  884.  
  885.         if (zoommode != ZoomNone) SelectZoom();
  886.  
  887.         SetPolygons(polygons);
  888.  
  889.         Redraw(SelAll, TRUE);
  890.         editflag = 0;
  891.         SetCaption();
  892.     }
  893. }
  894.  
  895. void DesignData::FileNew(void)
  896. {
  897.     Parts *next, *p;
  898.     for (p = parts; p != NULL; p = next) {
  899.         next = p->next;
  900.         delete p;
  901.     }
  902.     filename[0] = '\0';
  903.     SetCaption();
  904.     select = NULL;
  905.     parts = NULL;
  906.     if (zoommode != ZoomNone) SelectZoom();
  907.  
  908.     displayscale = 0.125;
  909.     displayoffset = Vector(0.0, 0.0, 0.0);
  910.     meshspacing = 200;
  911.     selectstatus = SelNone;
  912.     camera->position = Vector(1200,-900,900);
  913.     camera->SetTarget(Vector(0,0,0));
  914.  
  915.     SetPolygons(0);
  916.     Redraw(SelAll, TRUE);
  917.     editflag = 0;
  918. }
  919.  
  920. void DesignData::SetCaption(void)
  921. {
  922.     TApplication *app = TopView->GetApplication();
  923.     TWindow *frame = app->GetMainWindow();
  924.     if (filename[0]) {
  925.         char str[_MAX_PATH];
  926.         sprintf(str, "%s - %s", app->GetName(), filename);
  927.         frame->SetCaption(str);
  928.     } else {
  929.         frame->SetCaption(app->GetName());
  930.     }
  931. }
  932.  
  933. TStatusBar *DesignData::InitStatusBar(void)
  934. {
  935.     statusbar = new TStatusBar(0, TGadget::Recessed);
  936.     statusbar_polygons = new TTextGadget(0, TGadget::Recessed, TTextGadget::Right, 8);
  937.     statusbar_polygons->SetText("Total:  0面");
  938. //    button_ok = new TButton(0, , "確定", 0, 0, 32, 16);
  939. //    button_ok = new TButton(Frame, BUTTON_OK, "確定", 0, 0, 34, 18);
  940. //    statusbar_button = new TControlGadget(*button_ok);
  941.     statusbar->Insert(*statusbar_polygons);
  942. //    statusbar->Insert(*statusbar_button);
  943.     return statusbar;
  944. }
  945.  
  946. void DesignData::SetPolygons(int p)
  947. {
  948.     polygons = p;
  949.     char str[16];
  950.     sprintf(str, "Total:%4d面", p);
  951.     statusbar_polygons->SetText(str);
  952. }
  953.  
  954. void DesignData::TotalScaling(double rate)
  955. {
  956.     for (Parts *p = parts; p != NULL; p = p->next) {
  957.         p->position *= rate;
  958.         p->scale *= rate;
  959.     }
  960.     Redraw(SelAll, TRUE);
  961. }
  962.  
  963. void DesignData::SelectAxis(SelectType t[3], Matrix& m)
  964. {
  965.     Vector vx = m.v[0].unit() ;
  966.     Vector vy = m.v[1].unit() ;
  967.     Vector vz = m.v[2].unit() ;
  968.  
  969.     if (vx.x < 0) vx.x = -vx.x;
  970.     if (vx.y < 0) vx.y = -vx.y;
  971.     if (vx.z < 0) vx.z = -vx.z;
  972.     if (vy.x < 0) vy.x = -vy.x;
  973.     if (vy.y < 0) vy.y = -vy.y;
  974.     if (vy.z < 0) vy.z = -vy.z;
  975.     if (vz.x < 0) vz.x = -vz.x;
  976.     if (vz.y < 0) vz.y = -vz.y;
  977.     if (vz.z < 0) vz.z = -vz.z;
  978.  
  979.     if (vx.z > vy.z && vx.z > vz.z) { // Z成分のもっとも少ないベクトルを選択
  980.         t[0] = SelX;
  981.         if (vy.y > vz.y) {            // Y成分のもっとも少ないベクトルを選択
  982.             t[1] = SelY;
  983.             t[2] = SelZ;
  984.         } else {
  985.             t[1] = SelZ;
  986.             t[2] = SelY;
  987.         }
  988.     } else if (vy.z > vz.z) {
  989.         t[0] = SelY;
  990.         if (vx.y > vz.y) {
  991.             t[1] = SelX;
  992.             t[2] = SelZ;
  993.         } else {
  994.             t[1] = SelZ;
  995.             t[2] = SelX;
  996.         }
  997.     } else {
  998.         t[0] = SelZ;
  999.         if (vx.y > vy.y) {
  1000.             t[1] = SelX;
  1001.             t[2] = SelY;
  1002.         } else {
  1003.             t[1] = SelY;
  1004.             t[2] = SelX;
  1005.         }
  1006.     }
  1007. //    t[2] = SelXYZ - t[0] - t[1];
  1008. }
  1009.  
  1010. void DesignData::SelectParts(Parts *p)
  1011. {
  1012.     if (p == select) {
  1013.         return;
  1014.     }
  1015.     select = p;
  1016.     if (select == camera) {
  1017.         backup_position = camera->position;
  1018.         backup_rotation = camera->target;
  1019.     } else if (select != NULL) {
  1020.         backup_position = select->position;
  1021.         backup_rotation = select->rotation;
  1022.         backup_scale    = select->scale;
  1023.     }
  1024. }
  1025.  
  1026. void DesignData::CancelParts(void)
  1027. {
  1028.     if (select == NULL) {
  1029.         return;
  1030.     }
  1031.     if (select == camera) {
  1032.         camera->position = backup_position;
  1033.         camera->SetTarget(backup_rotation);
  1034.         select = NULL;
  1035.         Redraw(SelAll, TRUE);
  1036.     } else if (select == combined) {
  1037. #if 0
  1038.         Matrix m0 = Matrix::m_move(combined->orig_position);
  1039.         Matrix m1 = combined->GetMatrix();
  1040.         combined->MoveParts(m0 * m1.inv(), 1.0);
  1041. #else
  1042.         combined->position = combined->orig_position;
  1043.         combined->rotation = Vector(0,0,0);
  1044.         combined->scale = Vector(1,1,1);
  1045.         combined->MoveParts();
  1046. #endif
  1047.         CalcPoints(select);
  1048.         select = NULL;
  1049.         Redraw();
  1050.     } else {
  1051.         select->position = backup_position;
  1052.         select->rotation = backup_rotation;
  1053.         select->scale    = backup_scale;
  1054.         CalcPoints(select);
  1055.         select = NULL;
  1056.         Redraw();
  1057.     }
  1058. }
  1059.  
  1060. void DesignData::SelectZoom(void)
  1061. {
  1062.     if (zoommode == ZoomNone) {
  1063.         backup_displayscale = displayscale;
  1064.         backup_displayoffset = displayoffset;
  1065.         zoommode = ZoomSelect;
  1066. #if 0
  1067.         scrollX->Show(SW_HIDE);
  1068.         scrollY->Show(SW_HIDE);
  1069.         scrollZ->Show(SW_HIDE);
  1070. #endif
  1071.         statusbar->SetText("ズームしたい位置をクリックしてください");
  1072.         button_ok->Show(SW_HIDE);
  1073.         button_cancel->Show(SW_HIDE);
  1074.     } else {
  1075.         displayscale = backup_displayscale;
  1076.         displayoffset = backup_displayoffset;
  1077.         SetScrollLimit(TRUE);
  1078. //        scrollX->SetPosition(-displayoffset.x / scrollunit);
  1079. //        scrollY->SetPosition(-displayoffset.y / scrollunit);
  1080. //        scrollZ->SetPosition(-displayoffset.z / scrollunit);
  1081. #if 0
  1082.         scrollX->Show(SW_SHOW);
  1083.         scrollY->Show(SW_SHOW);
  1084.         scrollZ->Show(SW_SHOW);
  1085. #endif
  1086.         zoommode = ZoomNone;
  1087.         Redraw(SelXYZ | SelStat, TRUE);
  1088.     }
  1089. }
  1090.  
  1091. int DesignData::GetLinesNoBackFace(LineSegment **tmpline, PolyData *polydata)
  1092. {
  1093.     int W = PersView->Attr.W;
  1094.     int H = PersView->Attr.H;
  1095.     Parts *p;
  1096.     polydata->Init(W, H);
  1097.     for (p = parts; p != NULL; p= p->next) {
  1098.         if (p->object == NULL) continue;
  1099.  
  1100.         int inverseflag = FALSE;
  1101.         if (p->scale.x < 0) inverseflag = !inverseflag;
  1102.         if (p->scale.y < 0) inverseflag = !inverseflag;
  1103.         if (p->scale.z < 0) inverseflag = !inverseflag;
  1104.  
  1105.         int offp;
  1106.         offp = polydata->points;
  1107.         for (int i = 0; i < p->object->points; ++i) {
  1108.             polydata->point[i+offp].x = p->view_x[i];
  1109.             polydata->point[i+offp].y = p->view_y[i];
  1110.             polydata->point[i+offp].z = p->view_z[i];
  1111.         }
  1112.         polydata->points += p->object->points;
  1113.         for (i = 0; i < p->object->polys; ++i) {
  1114.             int *pid = p->object->polypoint + p->object->poly[i];
  1115.             PolyPoint *p1 = &polydata->point[pid[0]+offp];
  1116.             PolyPoint *p2 = &polydata->point[pid[1]+offp];
  1117.             PolyPoint *p3 = &polydata->point[pid[2]+offp];
  1118.             long m = (long)(p1->x-p2->x)*(long)(p3->y-p2->y) - (long)(p1->y-p2->y)*(long)(p3->x-p2->x);
  1119.             if (inverseflag && m <= 0) {
  1120.                polydata->AddPolyInv(p->object->polypoint + p->object->poly[i], offp, p);
  1121.             } else if (!inverseflag && m >= 0) {
  1122.                polydata->AddPoly(p->object->polypoint + p->object->poly[i], offp, p);
  1123.             }
  1124.         }
  1125.     }
  1126.  
  1127.     polydata->ConvertLines();
  1128.  
  1129.     int lines = 0;
  1130.     for (int i = 0; i < polydata->lines; ++i) {
  1131.         if (polydata->line[i].p1 >= 0
  1132.          && polydata->line[i].p2 >= 0) {
  1133.             PolyPoint &p1 = polydata->point[polydata->line[i].p1];
  1134.             PolyPoint &p2 = polydata->point[polydata->line[i].p2];
  1135.             if (p1.z > 0 && p2.z > 0
  1136.              && ((p1.x != p2.x) || (p1.y != p2.y))
  1137.              && (p1.x >= 0 || p2.x >= 0)
  1138.              && (p1.x <  W || p2.x <  W)
  1139.              && (p1.y >= 0 || p2.y >= 0)
  1140.              && (p1.y <  H || p2.y <  H)) {
  1141.                 lines++;
  1142.             }
  1143.         }
  1144.     }
  1145.  
  1146.     int l = 0;
  1147.     *tmpline = new LineSegment[lines];
  1148.     for (i = 0; i < polydata->lines; ++i) {
  1149.         if (polydata->line[i].p1 >= 0 && polydata->line[i].p2 >= 0) {
  1150.             PolyPoint &p1 = polydata->point[polydata->line[i].p1];
  1151.             PolyPoint &p2 = polydata->point[polydata->line[i].p2];
  1152.             if (p1.z > 0 && p2.z > 0
  1153.              && ((p1.x != p2.x) || (p1.y != p2.y))
  1154.              && (p1.x >= 0 || p2.x >= 0)
  1155.              && (p1.x <  W || p2.x <  W)
  1156.              && (p1.y >= 0 || p2.y >= 0)
  1157.              && (p1.y <  H || p2.y <  H)) {
  1158.                 (*tmpline)[l].x1 = (short)p1.x;
  1159.                 (*tmpline)[l].y1 = (short)p1.y;
  1160.                 (*tmpline)[l].x2 = (short)p2.x;
  1161.                 (*tmpline)[l].y2 = (short)p2.y;
  1162.                 (*tmpline)[l].id = polydata->line[i].id;
  1163.                 l++;
  1164.             }
  1165.         }
  1166.     }
  1167.     return l;
  1168. }
  1169.  
  1170. void DesignData::BuildWireFrame(void)
  1171. {
  1172.     if (!hiddenremove) {
  1173.         return;
  1174.     }
  1175.     if (wireframe.line != NULL) {
  1176.         delete[] wireframe.line;
  1177.         wireframe.line = NULL;
  1178.     }
  1179.     int maxlines = 0;
  1180.     int maxpolys = 0;
  1181.     int maxpoints = 0;
  1182.     int maxpolypoints = 0;
  1183.     for (Parts *p = parts; p != NULL; p = p->next) {
  1184.         if (p->object != NULL) {
  1185.             maxpoints += p->object->points;
  1186.             maxlines += p->lines;
  1187.             maxpolys += p->object->polys;
  1188.             maxpolypoints += p->object->polypoints;
  1189.         }
  1190.     }
  1191.     PolyData polydata(maxpoints, maxlines, maxpolys, maxpolypoints);
  1192.  
  1193.     int l = GetLinesNoBackFace(&wireframe.line, &polydata);
  1194.     wireframe.lines = l;
  1195. }
  1196.  
  1197.