//---------------------------------------------------------------------------

#pragma hdrstop

//---------------------------------------------------------------------------
#pragma package(smart_init)
#include <vcl.h>
#include <math.h>
#include "Point_dvutavr.h"
#include "Data_Gen.h"
#include "ProcExt.h"
#include "Mltmm.h"
#include "ListConst.h"
#include "Get_param_sect_prof.h"
#include "Draw3D_beam.h"


static int X_center;
static int Y_center;
static float Scale_mod;
static double T_conv[4][4];

//--------------------------------------------------
// получить точки контура сварного двутавра
void  Point_dvutavr(int zero, int zero1, int zero2, SECT_DVUTAVR *sect, float scale, TPoint *vertices) {
  vertices[0] = Point(zero2, zero);
  vertices[1] = Point(zero2+sect->b1*scale, zero);
  vertices[2] = Point(zero2+sect->b1*scale, zero+sect->h1*scale);
  vertices[3] = Point(zero2+0.5*(sect->b1+sect->b)*scale, zero+sect->h1*scale);
  vertices[4] = Point(zero2+0.5*(sect->b1+sect->b)*scale, zero+(sect->h + sect->h1)*scale);
  vertices[5] = Point(zero1+sect->b2*scale, zero+(sect->h + sect->h1)*scale);
  vertices[6] = Point(zero1+sect->b2*scale, zero+(sect->h + sect->h1 + sect->h2)*scale);
  vertices[7] = Point(zero1, zero+(sect->h + sect->h1 + sect->h2)*scale);
  vertices[8] = Point(zero1, zero+(sect->h + sect->h1)*scale);
  vertices[9] = Point(zero2+0.5*(sect->b1-sect->b)*scale, zero+(sect->h + sect->h1)*scale);
  vertices[10] = Point(zero2+0.5*(sect->b1-sect->b)*scale, zero+(sect->h1)*scale);
  vertices[11] = Point(zero2, zero+(sect->h1)*scale);
  vertices[12] = Point(zero2, zero);
}
//--------------------------------------------------
// получить точки контура сварного тавра
void  Point_tavr(int zero, int zero1, int zero2, SECT_TAVR *sect, float scale, TPoint *vertices) {
  vertices[0] = Point(zero2, zero);
  vertices[1] = Point(zero2+sect->b1*scale, zero);
  vertices[2] = Point(zero2+sect->b1*scale, zero+sect->h1*scale);
  vertices[3] = Point(zero2+0.5*(sect->b1+sect->b)*scale, zero+sect->h1*scale);
  vertices[4] = Point(zero2+0.5*(sect->b1+sect->b)*scale, zero+(sect->h + sect->h1)*scale);
  vertices[5] = Point(zero2+0.5*(sect->b1-sect->b)*scale, zero+(sect->h + sect->h1)*scale);
  vertices[6] = Point(zero2+0.5*(sect->b1-sect->b)*scale, zero+(sect->h1)*scale);
  vertices[7] = Point(zero2, zero+(sect->h1)*scale);
  vertices[8] = Point(zero2, zero);
}
//---------------------------------------------------------------------------
// Получить координаты сварного двутавра
void  Coor_weld_dvutavr(SECT_DVUTAVR *sect, double coor[][3]) {
  double b1c = sect->b1;
  double b1p = 0.5*(sect->b1 + sect->b);
  double b1m = 0.5*(sect->b1 - sect->b);
  double b2c = (sect->b1 + sect->b2)/2;
  double b20 = (sect->b1 - sect->b2)/2;
  double h1c = sect->h1;
  double h_h1 = (sect->h + sect->h1);
  double h_2h1 = (sect->h + sect->h1 + sect->h2);

  coor[0][0] = 0;          coor[0][1] = 0;          coor[0][2] = 0;
  coor[1][0] = b1c;        coor[1][1] = 0;          coor[1][2] = 0;
  coor[2][0] = b1c;        coor[2][1] = h1c;        coor[2][2] = 0;
  coor[3][0] = b1p;        coor[3][1] = h1c;        coor[3][2] = 0;
  coor[4][0] = b1p;        coor[4][1] = h_h1;       coor[4][2] = 0;
  coor[5][0] = b2c;        coor[5][1] = h_h1;       coor[5][2] = 0;
  coor[6][0] = b2c;        coor[6][1] = h_2h1;      coor[6][2] = 0;
  coor[7][0] = b20;        coor[7][1] = h_2h1;      coor[7][2] = 0;
  coor[8][0] = b20;        coor[8][1] = h_h1;       coor[8][2] = 0;
  coor[9][0] = b1m;        coor[9][1] = h_h1;       coor[9][2] = 0;
  coor[10][0] = b1m;       coor[10][1] = h1c;       coor[10][2] = 0;
  coor[11][0] = 0;         coor[11][1] = h1c;       coor[11][2] = 0;
  coor[12][0] = 0;         coor[12][1] = 0;         coor[12][2] = 0;

}
//---------------------------------------------------------
// Получить координаты прокатного двутавра
void  Coor_stand_dvutavr(SECT_DVUTAVR *sect, double rad, double coor[][3]) {
  double r_c = rad;
  double r2_c = Round(r_c - r_c*cos(45/57.3), 2);
  double b1c = sect->b1;
  double b1p = 0.5*(sect->b1 + sect->b);
  double b1m = 0.5*(sect->b1 - sect->b);
  double h1c = sect->h1;
  double h_h1 = (sect->h + sect->h1);
  double h_2h1 = (sect->h + 2*sect->h1);

  coor[0][0] = 0;          coor[0][1] = 0;          coor[0][2] = 0;
  coor[1][0] = b1c;        coor[1][1] = 0;          coor[1][2] = 0;
  coor[2][0] = b1c;        coor[2][1] = h1c;        coor[2][2] = 0;
  coor[3][0] = b1p + r_c;  coor[3][1] = h1c;        coor[3][2] = 0;
  coor[4][0] = b1p + r2_c; coor[4][1] = h1c + r2_c; coor[4][2] = 0;
  coor[5][0] = b1p;        coor[5][1] = h1c + r_c;  coor[5][2] = 0;
  coor[6][0] = b1p;        coor[6][1] = h_h1 - r_c;  coor[6][2] = 0;
  coor[7][0] = b1p + r2_c; coor[7][1] = h_h1 - r2_c; coor[7][2] = 0;
  coor[8][0] = b1p + r_c;  coor[8][1] = h_h1;       coor[8][2] = 0;
  coor[9][0] = b1c;        coor[9][1] = h_h1;       coor[9][2] = 0;
  coor[10][0] = b1c;       coor[10][1] = h_2h1;     coor[10][2] = 0;
  coor[11][0] = 0;         coor[11][1] = h_2h1;     coor[11][2] = 0;
  coor[12][0] = 0;         coor[12][1] = h_h1;      coor[12][2] = 0;
  coor[13][0] = b1m - r_c; coor[13][1] = h_h1;      coor[13][2] = 0;
  coor[14][0] = b1m - r2_c;coor[14][1] = h_h1 - r2_c; coor[14][2] = 0;
  coor[15][0] = b1m;       coor[15][1] = h_h1 - r_c; coor[15][2] = 0;
  coor[16][0] = b1m;       coor[16][1] = h1c + r_c;  coor[16][2] = 0;
  coor[17][0] = b1m - r2_c;coor[17][1] = h1c + r2_c; coor[17][2] = 0;
  coor[18][0] = b1m - r_c; coor[18][1] = h1c;        coor[18][2] = 0;
  coor[19][0] = 0;         coor[19][1] = h1c;        coor[19][2] = 0;
  coor[20][0] = 0;         coor[20][1] = 0;          coor[20][2] = 0;
}

//----------------------------------------------------------------------------
// Получить точки контура прокатного двутавра (старый вариант)
void  Point_stand_dvutavr(int zero, int zero1, int zero2, SECT_DVUTAVR *sect, double rad, float scale, TPoint *vertices) {
  double r_c = rad*scale;
  double r2_c = Round(r_c - r_c*cos(45/57.3), 0);
  double b1c = sect->b1*scale;
  double b1p = 0.5*(sect->b1 + sect->b)*scale;
  double b1m = 0.5*(sect->b1 - sect->b)*scale;
  double h1c = sect->h1*scale;
  double h_h1 = (sect->h + sect->h1)*scale;
  double h_2h1 = (sect->h + 2*sect->h1)*scale;

  vertices[0] = Point(zero2, zero);
  vertices[1] = Point(zero2 + b1c, zero);
  vertices[2] = Point(zero2 + b1c, zero + h1c);
  vertices[3] = Point(zero2 + b1p + r_c, zero + h1c);
  vertices[4] = Point(zero2 + b1p + r2_c, zero + h1c + r2_c);
  vertices[5] = Point(zero2 + b1p, zero + h1c + r_c);
  vertices[6] = Point(zero2 + b1p, zero + h_h1 - r_c);
  vertices[7] = Point(zero1 + b1p + r2_c, zero + h_h1 - r2_c);
  vertices[8] = Point(zero1 + b1p + r_c, zero + h_h1);
  vertices[9] = Point(zero1 + b1c, zero + h_h1);
  vertices[10] = Point(zero1 + b1c, zero + h_2h1);
  vertices[11] = Point(zero1, zero + h_2h1);
  vertices[12] = Point(zero1, zero + h_h1);
  vertices[13] = Point(zero2 + b1m - r_c, zero + h_h1);
  vertices[14] = Point(zero2 + b1m - r2_c, zero + h_h1 - r2_c);
  vertices[15] = Point(zero2 + b1m , zero + h_h1 - r_c);
  vertices[16] = Point(zero2 + b1m, zero + h1c + r_c);
  vertices[17] = Point(zero2 + b1m - r2_c, zero + h1c + r2_c);
  vertices[18] = Point(zero2 + b1m - r_c, zero + h1c);
  vertices[19] = Point(zero2, zero + h1c);
  vertices[20] = Point(zero2, zero);
}
//----------------------------------------------------------------------------
// Получить точки контура сварного двутавра
void  Point_weld_dvutavr(int zero, int zero1, int zero2, SECT_DVUTAVR *sect, float scale, TPoint *vertices) {
  double b1c = sect->b1*scale;
  double b1p = 0.5*(sect->b1 + sect->b)*scale;
  double b1m = 0.5*(sect->b1 - sect->b)*scale;
  double b2c = sect->b2*scale;
  double b2p = 0.5*(sect->b2 + sect->b)*scale;
  double b2m = 0.5*(sect->b2 - sect->b)*scale;
  double h1c = sect->h1*scale;
  double h_h1 = (sect->h + sect->h1)*scale;
  double h_2h1 = (sect->h + sect->h1 + sect->h2)*scale;
  double h2c = sect->h2*scale;
  double h_h2 = (sect->h + sect->h2)*scale;
  double h_2h2 = (sect->h + 2*sect->h2)*scale;

  vertices[0] = Point(zero2, zero);
  vertices[1] = Point(zero2 + b1c, zero);
  vertices[2] = Point(zero2 + b1c, zero + h1c);
  vertices[3] = Point(zero2 + b1p, zero + h1c);
  vertices[4] = Point(zero2 + b1p, zero + h_h1);
  vertices[5] = Point(zero1 + b2c, zero + h_h1);
  vertices[6] = Point(zero1 + b2c, zero + h_2h1);
  vertices[7] = Point(zero1, zero + h_2h1);
  vertices[8] = Point(zero1, zero + h_h1);
  vertices[9] = Point(zero2 + b1m, zero + h_h1);
  vertices[10] = Point(zero2 + b1m, zero + h1c);
  vertices[11] = Point(zero2, zero + h1c);
  vertices[12] = Point(zero2, zero);
}
//---------------------------------------------------------------------------
// Точки прямоугольной трубы
void Coor_rect(double h_c, double b_c, double r_c, double r2_c, double t_, double coor[][3]) {
  coor[0][0] = t_;              coor[0][1] = r_c;          coor[0][2] = 0;
  coor[1][0] = t_;              coor[1][1] = h_c - r_c;    coor[1][2] = 0;
  coor[2][0] = r2_c + t_*0.7;   coor[2][1] = h_c - r2_c - 0.7*t_;    coor[2][2] = 0;
  coor[3][0] = r_c;             coor[3][1] = h_c - t_;          coor[3][2] = 0;
  coor[4][0] = b_c - r_c;       coor[4][1] = h_c - t_;          coor[4][2] = 0;
  coor[5][0] = b_c - r2_c - t_*0.7; coor[5][1] = h_c - r2_c - 0.7*t_;   coor[5][2] = 0;
  coor[6][0] = b_c - t_;        coor[6][1] = h_c - r_c;    coor[6][2] = 0;
  coor[7][0] = b_c - t_;        coor[7][1] = r_c;          coor[7][2] = 0;
  coor[8][0] = b_c - r2_c - t_*0.7; coor[8][1] = r2_c + 0.7*t_;         coor[8][2] = 0;
  coor[9][0] = b_c - r_c;       coor[9][1] = t_;            coor[9][2] = 0;
  coor[10][0] = r_c;            coor[10][1] = t_;           coor[10][2] = 0;
  coor[11][0] = r2_c + t_*0.7;  coor[11][1] = r2_c + 0.7*t_;        coor[11][2] = 0;
  coor[12][0] = t_;             coor[12][1] = r_c;         coor[12][2] = 0;
}
//---------------------------------------------------------------------------
// Точки круглой трубы
void Coor_circ(int n, double rad0, double t_, double coor[][3]) {
  double alfa = 0;
  double delta;
  int i;
  double rad;
  delta = 360/(RADIAN*n);
  rad = rad0 - t_;
  coor[0][0] = 0;          coor[0][1] = rad;          coor[0][2] = 0;

  for (i=1; i<n; i++) {
	alfa += delta;
	coor[i][0] = rad*sin(alfa);          coor[i][1] = rad*cos(alfa);    coor[i][2] = 0;
  }
  coor[i][0] = 0;          coor[i][1] = rad;          coor[i][2] = 0;
  // центр
  coor[i+1][0] = 0;        coor[i+1][1] = 0;          coor[i+1][2] = 0;

}
//---------------------------------------------------------------------------
// Точки прямоугольной трубы
void Point_rect(TPoint *vertices, double zero, double zero1, double h_c, double b_c, double r_c, double r2_c) {
  vertices[0] = Point(zero, zero1 + r_c);
  vertices[1] = Point(zero, zero1 + h_c - r_c);
  vertices[2] = Point(zero + r2_c, zero1 + h_c - r2_c);
  vertices[3] = Point(zero + r_c, zero1 + h_c);
  vertices[4] = Point(zero + b_c - r_c, zero1 + h_c);
  vertices[5] = Point(zero + b_c - r2_c, zero1 + h_c - r2_c);
  vertices[6] = Point(zero + b_c, zero1 + h_c - r_c);
  vertices[7] = Point(zero + b_c, zero1 + r_c);
  vertices[8] = Point(zero + b_c - r2_c, zero1 + r2_c);
  vertices[9] = Point(zero + b_c - r_c, zero1);
  vertices[10] = Point(zero + r_c, zero1);
  vertices[11] = Point(zero + r2_c, zero1 + r2_c);
  vertices[12] = Point(zero, zero1 + r_c);
}
//---------------------------------------------------------
// Получить координаты прокатного тавра
void  Coor_stand_tavr(SECT_TAVR *sect, double rad, double coor[][3], int index_composed) {
  double r_c = rad;
  double r2_c = Round(r_c - r_c*cos(45/57.3), 0);            //sect[0]
  double b1c = sect->b1;
  double b1p = 0.5*(sect->b1 + sect->b);
  double b1m = 0.5*(sect->b1 - sect->b);
  double h1c = sect->h1;
  double h_h1 = (sect->h + sect->h1);

  if (index_composed==0) {
  coor[0][0] = 0;          coor[0][1] = 0;          coor[0][2] = 0;
  coor[1][0] = b1c;        coor[1][1] = 0;          coor[1][2] = 0;
  coor[2][0] = b1c;        coor[2][1] = h1c;        coor[2][2] = 0;
  coor[3][0] = b1p + r_c;  coor[3][1] = h1c;        coor[3][2] = 0;
  coor[4][0] = b1p + r2_c; coor[4][1] = h1c + r2_c; coor[4][2] = 0;
  coor[5][0] = b1p;        coor[5][1] = h1c + r_c;  coor[5][2] = 0;
  coor[6][0] = b1p;        coor[6][1] = h_h1;       coor[6][2] = 0;
  coor[7][0] = b1m;        coor[7][1] = h_h1;       coor[7][2] = 0;
  coor[8][0] = b1m;        coor[8][1] = h1c + r_c;  coor[8][2] = 0;
  coor[9][0] = b1m - r2_c; coor[9][1] = h1c + r2_c; coor[9][2] = 0;
  coor[10][0] = b1m - r_c; coor[10][1] = h1c;       coor[10][2] = 0;
  coor[11][0] = 0;         coor[11][1] = h1c;       coor[11][2] = 0;
  coor[12][0] = 0;         coor[12][1] = 0;         coor[12][2] = 0;

  }
  else {  // Полка снизу
	coor[0][0] = b1m;          coor[0][1] = 0;                       coor[0][2] = 0;
	coor[1][0] = b1p;          coor[1][1] = 0;                       coor[1][2] = 0;
	coor[2][0] = b1p;          coor[2][1] = h_h1 - h1c - r_c;        coor[2][2] = 0;
	coor[3][0] = b1p + r2_c;   coor[3][1] = h_h1 - h1c - r2_c;       coor[3][2] = 0;
	coor[4][0] = b1p + r_c;    coor[4][1] = h_h1 - h1c;              coor[4][2] = 0;
	coor[5][0] = b1c;          coor[5][1] = h_h1 - h1c;              coor[5][2] = 0;
	coor[6][0] = b1c;          coor[6][1] = h_h1;                    coor[6][2] = 0;
	coor[7][0] = 0;            coor[7][1] = h_h1;                    coor[7][2] = 0;
	coor[8][0] = 0;            coor[8][1] = h_h1 - h1c;              coor[8][2] = 0;
	coor[9][0] = b1m - r_c;    coor[9][1] = h_h1 - h1c;              coor[9][2] = 0;
	coor[10][0] = b1m - r2_c;  coor[10][1] = h_h1  - h1c - r2_c;     coor[10][2] = 0;
	coor[11][0] = b1m;         coor[11][1] = h_h1  - h1c - r_c;       coor[11][2] = 0;
	coor[12][0] = b1m;         coor[12][1] = 0;                      coor[12][2] = 0;
  }

}
//---------------------------------------------------------
// Добавить координаты пластины
void  Coor_plate(double width_prof, double height_prof, double plate_thick, double plate_width, int shift, double coor[][3],
				 int index_composed) {

  if (index_composed==0) {
	coor[shift][0] = -(plate_width - width_prof)/2 ;  coor[shift][1] = height_prof;                   coor[shift][2] = 0;
	coor[shift+1][0] = (plate_width + width_prof)/2;  coor[shift+1][1] = height_prof;                 coor[shift+1][2] = 0;
	coor[shift+2][0] = (plate_width + width_prof)/2;  coor[shift+2][1] = height_prof + plate_thick;   coor[shift+2][2] = 0;
	coor[shift+3][0] = -(plate_width - width_prof)/2; coor[shift+3][1] = height_prof + plate_thick;   coor[shift+3][2] = 0;
	coor[shift+4][0] = -(plate_width - width_prof)/2; coor[shift+4][1] = height_prof;                 coor[shift+4][2] = 0;
  }
  else {
	coor[shift][0] = -(plate_width - width_prof)/2 ;  coor[shift][1] = 0;                   coor[shift][2] = 0;
	coor[shift+1][0] = (plate_width + width_prof)/2;  coor[shift+1][1] = 0;                 coor[shift+1][2] = 0;
	coor[shift+2][0] = (plate_width + width_prof)/2;  coor[shift+2][1] = - plate_thick;     coor[shift+2][2] = 0;
	coor[shift+3][0] = -(plate_width - width_prof)/2; coor[shift+3][1] = - plate_thick;     coor[shift+3][2] = 0;
	coor[shift+4][0] = -(plate_width - width_prof)/2; coor[shift+4][1] = 0;                 coor[shift+4][2] = 0;

  }
}

//----------------------------------------------------------------------------
// Получить точки контура прокатного тавра (
void  Point_stand_tavr(int zero, int zero1, int zero2, SECT_TAVR *sect, double rad, float scale, TPoint *vertices, int index_comp) {
  double r_c = rad*scale;
  double r2_c = Round(r_c - r_c*cos(45/57.3), 0);            //sect[0]
  double b1c = sect->b1*scale;
  double b1p = 0.5*(sect->b1 + sect->b)*scale;
  double b1m = 0.5*(sect->b1 - sect->b)*scale;
  double h1c = sect->h1*scale;
  double h_h1 = (sect->h + sect->h1)*scale;
  TPoint vert[13];

  if (index_comp==0) {  // Полка сверху
	vertices[0] = Point(zero2, zero);
	vertices[1] = Point(zero2 + b1c, zero);
	vertices[2] = Point(zero2 + b1c, zero + h1c);
	vertices[3] = Point(zero2 + b1p + r_c, zero + h1c);
	vertices[4] = Point(zero2 + b1p + r2_c, zero + h1c + r2_c);
	vertices[5] = Point(zero2 + b1p, zero + h1c + r_c);
	vertices[6] = Point(zero2 + b1p, zero + h_h1);
	vertices[7] = Point(zero2 + b1m , zero + h_h1);
	vertices[8] = Point(zero2 + b1m, zero + h1c + r_c);
	vertices[9] = Point(zero2 + b1m - r2_c, zero + h1c + r2_c);
	vertices[10] = Point(zero2 + b1m - r_c, zero + h1c);
	vertices[11] = Point(zero2, zero + h1c);
	vertices[12] = Point(zero2, zero);
  }
  else {  // Полка снизу
	vertices[0] = vert[0] = Point(zero2 + b1m, zero);
	vertices[1] = vert[1] = Point(zero2 + b1p, zero);
	vertices[2] = vert[2] = Point(zero2 + b1p, zero + h_h1 - h1c - r_c);
	vertices[3] = vert[3] = Point(zero2 + b1p + r2_c, zero + h_h1 - h1c - r2_c);
	vertices[4] = vert[4] = Point(zero2 + b1p + r_c, zero + h_h1 - h1c);
	vertices[5] = vert[5] = Point(zero2 + b1c, zero + h_h1 - h1c);
	vertices[6] = vert[6] = Point(zero2 + b1c, zero + h_h1);
	vertices[7] = vert[7] = Point(zero2, zero + h_h1);
	vertices[8] = vert[8] = Point(zero2, zero + h_h1 - h1c);
	vertices[9] = vert[9] = Point(zero2 + b1m - r_c, zero + h_h1 - h1c);
	vertices[10] = vert[10] = Point(zero2 + b1m - r2_c, zero + h_h1  - h1c - r2_c);
	vertices[11] = vert[11] = Point(zero2 + b1m, zero + h_h1  - h1c - r_c);
	vertices[12] = vert[12] = Point(zero2 + b1m, zero);
  }
}
//---------------------------------------------------------
// Получить точки контура прокатного двутавра
void  Point_stand_dvutavr_n(int zero, int zero1, int zero2, SECT_DVUTAVR *sect, double rad, float scale, TPoint *vertices) {
  int i;
  double coor[21][3];
  // Получить координаты точек двутавра
  Coor_stand_dvutavr(sect, rad, coor);
  for (i=0; i<21; i++) {
	 vertices[i] = Point(zero1 + coor[i][0]*scale, zero + coor[i][1]*scale);
  }

}
//---------------------------------------------------------
// Получить точки тавровых профилей - проволочная модель
void  Point_tavr_wire(double *coor0, double alf, COMMON_SECT *sect, double scale, double **vertices, int *n_point) {
  double b1 = sect->tavr.b1*scale;
  double b1m = sect->tavr.h1*scale;
  double h0 = (sect->tavr.h + sect->tavr.h1)*scale;
  double b2m = sect->tavr.b*scale;             //sect[0]
  double fi0, delc;
  int i, k;
  double vec[3];
  double mrot[3][3] = {cos(alf), 0.0, sin(alf), 0.0, 1.0, 0.0, -sin(alf), 0.0, cos(alf)};

  GEOM_PARAM_SECT param_sect;

  Get_param_sect_prof(sect, &param_sect);

  if (sect->tavr.index_orient==0) {
    vertices[0][1] =   b1/2;
    vertices[0][2] =   -b1m/2;
    vertices[1][1] =   0;
    vertices[1][2] =   -b1m/2;
    vertices[2][1] =   -b1/2;
    vertices[2][2] =   -b1m/2;
    vertices[3][1] =   -0;
    vertices[3][2] =   -h0;
  }
  else {
    vertices[0][1] =   b1/2;
    vertices[0][2] =   -h0 + b1m/2;
    vertices[1][1] =   0;
    vertices[1][2] =   -h0 + b1m/2;
    vertices[2][1] =   -b1/2;
    vertices[2][2] =   -h0 + b1m/2;
    vertices[3][1] =   0;
    vertices[3][2] =   0;
  }
  *n_point = 4;
   for (i=0; i<*n_point; i++) {
     vertices[i][0] = 0;
     vertices[i][1] -= param_sect.Yct;
     vertices[i][2] += param_sect.Zct;
   }

   //prinm0("verices-----------------", *n_point, 3, vertices);

   for (i=0; i<*n_point; i++) {
       mltmv0(&(mrot[0][0]), 3, 3, vertices[i], 3, vec, 3);
       vertices[i][0] = vec[0];
       vertices[i][1] = vec[1];
       vertices[i][2] = vec[2];
   }
   //prinm0("verices-rot", *n_point, 3, vertices);
   for (i=0; i<*n_point; i++) {
       vertices[i][0] += coor0[0];
       vertices[i][1] += coor0[1];
       vertices[i][2] += coor0[2];
   }
   //prinm0("verices-shift", *n_point, 3, vertices);
}

//----------------------------------------------------------------------
// Вычисление точек зигзага
#define zigH   6
#define zigL   12
void  zigzag(TPoint *point, double dev, TPoint *vert) {
   int sx, sy;
   double alf;

   sx = (point[0].x + point[1].x)*dev;
   sy = (point[0].y + point[1].y)*dev;

   alf = atan2((double)(point[0].x - point[1].x), (double)(point[0].y - point[1].y));

   vert[0] = point[1];
   vert[1] = Point(sx - zigH*sin(alf), sy - zigH*cos(alf));
   vert[2] = Point(sx - zigH*0.5*sin(alf) + zigL*cos(alf), sy - zigH*0.5*cos(alf) + zigL*sin(alf));
   vert[3] = Point(sx + zigH*0.5*sin(alf) - zigL*cos(alf), sy + zigH*0.5*cos(alf) - zigL*sin(alf));
   vert[4] = Point(sx + zigH*sin(alf), sy + zigH*cos(alf));
   vert[5] = point[0];

}
//--------------------------------------------------------
// установить вид и вычислить матрицу преобразования
void SetView_(double t_conv[][4], double * z_mod, int Item, bool *Sign, int VertAx) {
// Item - проекция
// Sign[i]
// VertAx - номер оси диметрии
  double x0[3];
  double  x1[3];
  double Teta = 0;
  int i;
  #define   VIEWPOINT   1e10
  double ViewPoint = VIEWPOINT;

  switch (Item) {
  case 0:
  // Вид в плане с оси X
  x0[0]=1e10; x0[1]=x0[2]=0;
  if (Sign[0]==true)
	  x0[0]= -ViewPoint;
  break;
  case 1:
  // Вид в плане с оси Y
  x0[1]=1e10; x0[0]=x0[2]=0;
  if (Sign[1]==true)
      x0[1]= -ViewPoint;
  break;
  case 2:
  // Вид в плане с оси Z
  x0[2] = 1e10; x0[1] = x0[0] =0;
  if (Sign[2]==true)
      x0[2]= -ViewPoint;
  break;
  case vISOMETR:
  // Изометрия
  x0[0] = x0[1] = x0[2] = ViewPoint;
  if (Sign[0]==true)
	  x0[0]= -ViewPoint;
  if (Sign[1]==true)
	  x0[1]= -ViewPoint;
  if (Sign[2]==true)
	  x0[2]= -ViewPoint;
  break;
  case vISO_VERT:
  // Изометрия
  x0[0] = x0[1] = x0[2] = ViewPoint;
  Teta = 60;
  if (Sign[0]==true)
	  x0[0]= -ViewPoint;
  if (Sign[1]==true)
	  x0[1]= -ViewPoint;
  if (Sign[2]==true)
	  x0[2]= -ViewPoint;
  break;
  case 4:
  // Диметрия
  x0[1] = ViewPoint; x0[0] = x0[2] = 0.5*ViewPoint;
  //Teta = -129.25;
  if (VertAx==0) {
	 x0[0]=1e10; x0[1]=x0[2]=0.5*ViewPoint;
	 //Teta = -101.55;
  }
  if (VertAx==1) {
     x0[1]=ViewPoint; x0[0]=x0[2]=0.5*ViewPoint;
	 //Teta = -129.25;
  }
  if (VertAx==2) {
     x0[2]=ViewPoint;x0[0]=x0[1]=0.5*ViewPoint;
	 //Teta = -129.1;
  }
  if (Sign[0]==true)
	  x0[0] *= -1;
  if (Sign[1]==true)
	  x0[1] *= -1;
  if (Sign[2]==true)
	  x0[2] *= -1;
  break;
  }
  x1[0]=x1[1]=x1[2]=0;
  /* формирование матрицы преобразования */
  formt_conv(x0, x1, Teta, t_conv, z_mod);
  formt_conv(x0, x1, Teta, T_conv, z_mod); // Повторно

}
//----------------------------------------------------------------------------
// преобразовать координаты в экранную систему
void  Set_coorg_(double t_conv[][4], double coor[][3], int n_coor, double coorg[][3])
{
  int i,j;
  double cc[3];

   for (i=0; i<n_coor; i++){
     for(j=0; j<3; j++)
       cc[j]=t_conv[0][j]*coor[i][0]+
	   t_conv[1][j]*coor[i][1]+
       t_conv[2][j]*coor[i][2]+
       t_conv[3][j];
     for (j=0; j<3; j++) {
       coorg[i][j]=cc[j];
     }
   }
   //prinm0("Экранные координаты в Set_coorg0", n_coor, 3, coorg);

}
//---------------------------------------------------------------
// Сформировать матрицу преобразования координат
void formt_conv(double* x0, double* x1, double Teta, double t_conv[][4],
			 double *z_mod)
{
   double t1[4][4], t2[4][4], t3[4][4], t4[4][4];
   double tl1, tl2;
   int i,j;
      for (i=0; i<4; i++)
         for (j=0; j<4; j++)
             t1[i][j]=t2[i][j]=t3[i][j]=t4[i][j]=0e0;
      t1[0][0]=t1[1][1]=t1[2][2]=t1[3][3]=t2[1][1]=t2[3][3]=
      t3[0][0]=t3[3][3]=t4[2][2]=t4[3][3]=1e0;

       /* формирование матрицы смещений */
	  t1[3][0]= -x0[0];
      t1[3][1]= -x0[1];
      t1[3][2]= -x0[2];

     /* формирование матриц вращения */
      tl1=sqrt((x0[0]-x1[0])*(x0[0]-x1[0]) +
      (x0[2]-x1[2])*(x0[2]-x1[2]));
      tl2=sqrt(tl1*tl1+(x0[1]-x1[1])*(x0[1]-x1[1]));
      if (tl1!=0) {
		  t2[0][0]=t2[2][2]=(x1[2]-x0[2])/tl1;
          t2[0][2]=(x1[0]-x0[0])/tl1;
      }
      else
           t2[0][0]=t2[2][2]=1e0;
      t2[2][0]= - t2[0][2];
	  t3[1][1]=t3[2][2]=tl1/tl2;
	  t3[1][2]=(x1[1]-x0[1])/tl2;
      t3[2][1]= - t3[1][2];
	  t4[0][0]=t4[1][1]=cos(Teta/57.3);
	  t4[1][0]=sin(Teta/57.3);
	  t4[0][1]= -t4[1][0];

      //prinm_t("t1",t1);
	  //prinm_t("t2",t2);
	  //prinm_t("t3",t3);
      //prinm_t("t4",t4);

	  mltmm0((double*)t1,4,4,(double*)t2,4,4,(double*)t_conv,4,4);
	  //prinm_t("t_conv12",t_conv);
      mltmm0((double*)t_conv,4,4,(double*)t3,4,4,(double*)t1,4,4);
      //prinm_t("t_conv123",t1);
      mltmm0((double*)t1,4,4,(double*)t4,4,4,(double*)t_conv,4,4);
      //prinm_t("t_conv",t_conv);
      *z_mod=tl2;
}
//---------------------------------------------------------------------------------
// Получить масштаб и сдвиг
void Set_scale(int Width, int Height, double z_mod, double coor_1g[][3], double coor_2g[][3], int n,
			   double *Scale, double *X_shift, double *Y_shift, int  pole)
{
	int i;
	double xmax, ymax, xmin, ymin, zmax, xscale, yscale, scale, delta_z;
	double x1, y1, x2, y2;
	double xmod, ymod, X_mod, Y_mod;
	xmax = ymax = -1e12;
	xmin = ymin=  1e12;

	for (i=0, zmax=-1e0; i < n; i++) {
	  //zmax = MAX(1e0, coor_1g[i][2]);
	  //zmax = MAX(zmax, coor_2g[i][2]);
	  //delta_z = z_mod/zmax;
	  //coor_1g[i][0] *= delta_z;
	  //coor_1g[i][1] *= delta_z;
	  //coor_2g[i][0] *= delta_z;
	  //coor_2g[i][1] *= delta_z;
	  xmin = MIN(xmin, coor_1g[i][0]);
	  xmax = MAX(xmax, coor_1g[i][0]);
	  ymin = MIN(ymin, coor_1g[i][1]);
	  ymax = MAX(ymax, coor_1g[i][1]);
	  xmin = MIN(xmin, coor_2g[i][0]);
	  xmax = MAX(xmax, coor_2g[i][0]);
	  ymin = MIN(ymin, coor_2g[i][1]);
	  ymax = MAX(ymax, coor_2g[i][1]);

	}
	xscale = (xmax - xmin)/(Width - pole );
	yscale = (ymax - ymin)/(Height - pole);
	scale = MAX(xscale, yscale);
	X_mod = (xmax + xmin)*0.5;  // координаты центра модели в СК модели
	xmod = X_mod/scale;       // координаты центра модели в СК экрана
	Y_mod = (ymax + ymin)*0.5;
	ymod = Y_mod/scale;

	*Scale = 1/scale;
	// сдвиг координат рисунка
	*X_shift = xmod  - Width*0.5;
	*Y_shift = ymod  - Height*0.5;

	Scale_mod = *Scale;
	X_center = *X_shift;
	Y_center = *Y_shift;

}
//---------------------------------------------------------------
// Получить масштаб и сдвиги
void Get_scale(double *scale, double *x_shift, double *y_shift) {
  *scale = Scale_mod; *x_shift = X_center; *y_shift = Y_center;
}
//-----------------------------------------------------------------------
// получить экранные координаты точек для рисования нагрузок на геометрии
void get_point_load_ar(double load, double direction[3], double force_scale, double *coor_load, double t_ar, double l_ar,
					   double coor_g[][3], double *Sign) {
	 int i, j, k;
	 double coorg[3];
	 double SignLoad;
	 double coor_ar[10][3];
	 double mult, mult1;

	 if (direction[0]!=0) {
	   i =0; j = 1; k = 2;
	   mult  = direction[0]*pow(force_scale, 0.7)*SIGN(load);
	 }
	 if (direction[1]!=0) {
	   i = 1; j = 0; k = 2;
	   mult  = -direction[1]*pow(force_scale, 0.7)*SIGN(load);
	 }
	 if (direction[2]!=0) {
	   i = 2; j = 0; k = 1;
	   mult  = direction[2]*pow(force_scale, 0.7)*SIGN(load);
	 }
	 mult1 = 1;

	 coor_ar[0][i] = 0;               coor_ar[0][j] = 0;            coor_ar[0][k] = 0;
	 coor_ar[1][i] = 0;               coor_ar[1][j] =  mult1*t_ar/2;   coor_ar[1][k] = 0;
	 coor_ar[2][i] = mult*l_ar;        coor_ar[2][j] =  mult1*t_ar/2;   coor_ar[2][k] = 0;
	 coor_ar[3][i] = mult*l_ar;        coor_ar[3][j] =  mult1*t_ar*1.1;     coor_ar[3][k] = 0;
	 coor_ar[4][i] = mult*l_ar + SIGN(mult)*t_ar*1.1; coor_ar[4][j] = 0;       coor_ar[4][k] = 0;
	 coor_ar[5][i] = mult*l_ar;        coor_ar[5][j] = -mult1*t_ar*1.1;     coor_ar[5][k] = 0;
	 coor_ar[6][i] = mult*l_ar;        coor_ar[6][j] = -mult1*t_ar/2;   coor_ar[6][k] = 0;
	 coor_ar[7][i] = 0;               coor_ar[7][j] = -mult1*t_ar/2;   coor_ar[7][k] = 0;
	 coor_ar[8][i] = 0;               coor_ar[8][j] = 0;            coor_ar[8][k] = 0;

	 for (i=0; i<9; i++) {
	   coor_ar[i][0] += coor_load[0];
	   coor_ar[i][1] += coor_load[1];
	   coor_ar[i][2] += coor_load[2];
	 }
	 // преобразовать координаты в экранную систему
	 Set_coorg_(T_conv, coor_ar, 9, coor_g);

	 for (i=0; i<9; i++) {
		coor_g[i][0] = coor_g[i][0]* Scale_mod - X_center;
		coor_g[i][1] = coor_g[i][1]* Scale_mod - Y_center;
	 }

}
//-----------------------------------------------------------------------
// получить экранные координаты размерной линии
void get_point_dim(double direction[3], double *coor_load, double coor_g[][3]) {
	 int i, j, k;
	 double coorg[3];
	 double coor_ar[3][3];
	 double mult;

	 if (direction[0]!=0) {
	   i =0; j = 1; k = 2;
	   mult = -50*direction[0];
	 }
	 if (direction[1]!=0) {
	   i = 1; j = 0; k = 2;
	   mult = -50*direction[1];
	 }
	 if (direction[2]!=0) {
	   i = 2; j = 0; k = 1;
	   mult = -50*direction[2];
	 }

	 coor_ar[0][i] = 0;               coor_ar[0][j] = 0;            coor_ar[0][k] = 0;
	 coor_ar[1][i] = 0;               coor_ar[1][j] = mult;           coor_ar[1][k] = 0;

	 for (i=0; i<2; i++) {
	   coor_ar[i][0] += coor_load[0];
	   coor_ar[i][1] += coor_load[1];
	   coor_ar[i][2] += coor_load[2];
	 }
	 // преобразовать координаты в экранную систему
	 Set_coorg_(T_conv, coor_ar, 2, coor_g);

	 for (i=0; i<2; i++) {
		coor_g[i][0] = coor_g[i][0]* Scale_mod - X_center;
		coor_g[i][1] = coor_g[i][1]* Scale_mod - Y_center;
	 }

}
//-----------------------------------------------------------------------
// получить экранные координаты точек для рисования закреплений на геометрии
void get_point_constr(double direction[], double *coor_constr, double t_ar, double l_ar,
					   double coor_g[][3]) {
	 int i=0, j=1, k=2;
	 double coorg[3];
	 double coor_ar[10][3];
	 double mult;

	 if (direction[0]!=0) {
	   i =0; j = 1; k = 2;
	   mult = direction[0];
	 }
	 if (direction[1]!=0) {
	   i = 1; j = 0; k = 2;
	   mult = direction[1];
	 }
	 if (direction[2]!=0) {
	   i = 2; j = 0; k = 1;
	   mult = direction[2];
	 }
	 coor_ar[0][i] = 0;               coor_ar[0][j] = 0;         coor_ar[0][k] = 0;
	 coor_ar[1][i] = 0;               coor_ar[1][j] =  t_ar/2;   coor_ar[1][k] = 0;
	 coor_ar[2][i] = l_ar*mult;            coor_ar[2][j] =  t_ar/2;   coor_ar[2][k] = 0;
	 coor_ar[3][i] = l_ar*mult;            coor_ar[3][j] = 0;         coor_ar[3][k] = 0;
	 coor_ar[4][i] = l_ar*mult;            coor_ar[4][j] = -t_ar/2;   coor_ar[4][k] = 0;
	 coor_ar[5][i] = 0;               coor_ar[5][j] = -t_ar/2;   coor_ar[5][k] = 0;
	 coor_ar[6][i] = 0;               coor_ar[6][j] = 0;         coor_ar[6][k] = 0;

	 for (i=0; i<7; i++) {
	   coor_ar[i][0] += coor_constr[0];
	   coor_ar[i][1] += coor_constr[1];
	   coor_ar[i][2] += coor_constr[2];
	 }
	 // преобразовать координаты в экранную систему
	 Set_coorg_(T_conv, coor_ar, 7, coor_g);

	 for (i=0; i<7; i++) {
		coor_g[i][0] = coor_g[i][0]*Scale_mod - X_center;
		coor_g[i][1] = coor_g[i][1]*Scale_mod - Y_center;
	 }

}
//-----------------------------------------------------------------------
// получить экранные координаты точек для рисования нагрузок на геометрии
void get_point_load(double load, double direction[3], double force_scale, double *coor_load,
					double *x0, double *x1, double *x2, double *Sign) {
	 int j;
	 double cc[2];
	 double xp[2];
	 double coorg[3];
	 double delta_z, ss;
	 double SignLoad;



	 for(j=0; j<3; j++) {
		cc[j] = T_conv[0][j]*direction[0] + T_conv[1][j]*direction[1] +
				T_conv[2][j]*direction[2] + T_conv[3][j];
	 }
	 xp[0] = cc[0];
	 xp[1] = cc[1];


	 SignLoad = load*force_scale;
	 // Учет того, что нагрузка должна быть направлена в противоположную сторону
	 //if (load->load_A >0)
	 //SignLoad *= -1;
	 for(j=0; j<3; j++) {
	   coorg[j] = T_conv[0][j]*coor_load[0]+
				  T_conv[1][j]*coor_load[1]+
				  T_conv[2][j]*coor_load[2]+
				  T_conv[3][j];
	 }
	 //delta_z=z_mod/coorg[2];
	 //coorg[0] *= delta_z;
	 //coorg[1] *= -delta_z;


	 x0[0] = coorg[0] * Scale_mod - X_center;
	 x0[1] = coorg[1] * Scale_mod - Y_center;
	 x1[0] = x0[0] + xp[0]*SignLoad;
	 x1[1] = x0[1] + xp[1]*SignLoad;
	 if (SignLoad==0)  {
	   x2[0] = x0[0];
	   x2[1] = x0[1];
	 }
	 else {
	   x2[0] = x1[0] + 5*(x1[0] - x0[0])/(ABS(x1[0] - x0[0]));
	   x2[1] = x1[1] + 5*(x1[1] - x0[1])/(ABS(x1[1] - x0[1]));
	   //x2[0] = x1[0] + 3;
	   //x2[1] = x1[1] + 3;
	 }
	 *Sign = SignLoad;
}
//-----------------------------------------------------------------------
// получить экранные координаты точек для рисования дуги момента
void get_point_moment(double load, double *direction, double *coor_load, double coor_g[][3], double rad_8) {
	 double coor_m[20][3];
	 double alfa = 270/RADIAN;
	 double delta;
	 int i;
	 double rad;
	 delta = 180/(RADIAN*20);
	 rad = rad_8/8;

	 for (i=0; i<20; i++) {
	   alfa += delta;
	   if (direction[0]!=0) {
		  coor_m[i][1] = -rad*sin(alfa);          coor_m[i][2] = -direction[0]*rad*cos(alfa);    coor_m[i][0] = 0;
	   }
	   if (direction[1]!=0) {
		  coor_m[i][0] = -rad*sin(alfa);          coor_m[i][2] = -direction[1]*rad*cos(alfa);    coor_m[i][1] = 0;
	   }
	   if (direction[2]!=0) {
		  coor_m[i][0] = rad*sin(alfa);          coor_m[i][1] = direction[2]*rad*cos(alfa);    coor_m[i][2] = 0;
	   }
	   coor_m[i][0] += coor_load[0];
	   coor_m[i][1] += coor_load[1];
	   coor_m[i][2] += coor_load[2];
	 }

	 // преобразовать координаты в экранную систему
	 Set_coorg_(T_conv, coor_m, 20, coor_g);

	 for (i=0; i<20; i++) {
		coor_g[i][0] = coor_g[i][0]* Scale_mod - X_center;
		coor_g[i][1] = coor_g[i][1]* Scale_mod - Y_center;
	 }
}
//-----------------------------------------------------------------------
// получить экранные координаты точек распределенной нагрузки
void get_point_q(double coor_g[][3]) {
	double coor_d[2][3];
	 // преобразовать координаты в экранную систему
	 Set_coorg_(T_conv, coor_g, 2, coor_d);

	 for (int i=0; i<2; i++) {
		coor_g[i][0] = coor_d[i][0]* Scale_mod - X_center;
		coor_g[i][1] = coor_d[i][1]* Scale_mod - Y_center;
	 }
}
//-----------------------------------------------------------------------
// получить масштаб модели
double get_scale_mod() {
	return Scale_mod;
}
// получить координаты точек дуги
void get_point_arc(int n_point, double coor_g[][3], double rad, double angle, double angle_null) {
	 double alfa = angle_null/RADIAN;
	 double delta;
	 int i;
	 delta = angle/(RADIAN*n_point);

	 for (i=0; i<=n_point; i++) {
	   coor_g[i][0] = rad*cos(alfa);
	   coor_g[i][1] = rad*sin(alfa);
	   coor_g[i][2] = 0;
	   alfa += delta;
	 }
}


