"Fossies" - the Fresh Open Source Software Archive

Member "openscad-2019.05/libraries/MCAD/3d_triangle.scad" (24 Dec 2018, 13266 Bytes) of package /linux/privat/openscad-2019.05.src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) OpenSCAD source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 //    Enhancement of OpenSCAD Primitives Solid with Trinagles 
    2 //    Copyright (C) 2011  Rene BAUMANN, Switzerland
    3 //
    4 //    This library is free software; you can redistribute it and/or
    5 //    modify it under the terms of the GNU Lesser General Public
    6 //    License as published by the Free Software Foundation; either
    7 //    version 2.1 of the License, or (at your option) any later version.
    8 //
    9 //    This library is distributed in the hope that it will be useful,
   10 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   12 //    Lesser General Public License for more details.
   13 //
   14 //    You should have received a copy of the GNU Lesser General Public
   15 //    License along with this library; If not, see <http://www.gnu.org/licenses/>
   16 //    or write to the Free Software Foundation, Inc., 
   17 //    51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   18 // ================================================================
   19 //
   20 //  File providing functions and modules to draw 3D - triangles 
   21 //  created in the X-Y plane with hight h, using various triangle 
   22 //  specification methods. 
   23 //  Standard traingle geometrical definition  is used. Vertices are named A,B,C, 
   24 //  side a is opposite vertex A a.s.o. the angle at vertex A is named alpha, 
   25 //  B(beta), C(gamma).
   26 //
   27 //  This SW is a contribution to the Free Software Community doing a marvelous
   28 //  job of giving anyone access to knowledge and tools to educate himselfe.
   29 //
   30 //  Author:     Rene Baumann
   31 //  Date:       11.09.2011
   32 //  Edition:    0.3 11.09.2011 For review by Marius
   33 //  Edition:    0.4 11.11.2011 Ref to GPL2.1 added
   34 //
   35 // --------------------------------------------------------------------------------------
   36 //
   37 // ===========================================
   38 //
   39 //  FUNCTION:       3dtri_sides2coord
   40 //  DESCRIPTION:
   41 //      Enter triangle sides a,b,c and to get the A,B,C - corner
   42 //      co-ordinates. The trinagle's c-side lies on the x-axis
   43 //      and A-corner in the co-ordinates center [0,0,0]. Geometry rules
   44 //      required that a + b is greater then c.  The traingle's vertices are
   45 //      computed such that it is located in the X-Y plane,  side c is on the 
   46 //      positive x-axis. 
   47 //  PARAMETER:
   48 //      a   : real      length of side a
   49 //      b   : real      length of side b
   50 //      c   : real      length of side c
   51 //  RETURNS:
   52 //      vertices : [Acord,Bcord,Ccord] Array of vertices coordinates
   53 //
   54 //  COMMENT:
   55 //      vertices = 3dtri_sides2coord (3,4,5);
   56 //      vertices[0] : Acord vertex A cordinates the like [x,y,z] 
   57 // -------------------------------------------------------------------------------------    
   58 //
   59 function 3dtri_sides2coord (a,b,c) = [
   60         [0,0,0],
   61         [c,0,0], 
   62         [(pow(c,2)+pow(a,2)-pow(b,2))/(2*c),sqrt ( pow(a,2) - 
   63            pow((pow(c,2)+pow(a,2)-pow(b,2))/(2*c),2)),0]];
   64 //
   65 //
   66 // ===========================================
   67 //
   68 //  FUNCTION:       3dtri_centerOfGravityCoord
   69 //  DESCRIPTION:
   70 //      Enter triangle  A,B,C - corner coordinates to get the
   71 //      triangles Center of Gravity coordinates. It is assumed
   72 //      the triangle is parallel to the  X-Y plane. The function 
   73 //      returns always zero for the z-coordinate
   74 //  PARAMETER:
   75 //      Acord   : [x,y,z]   Coordinates of vertex A
   76 //      Bcord   : [x,y,z]   Coordinates of vertex B
   77 //      Ccord   : [x,y,z]   Coordinates of vertex C
   78 //  RETURNS:
   79 //      CG : [x,y,0]    Center of gravity coordinate in X-Y-plane
   80 //
   81 //  COMMENT:
   82 //      vertices = 3dtri_sides2coord (3,4,5);
   83 //      cg = 3dtri_centerOfGravityCoord(vertices[0],vertices[1],vertices[2]);
   84 // -------------------------------------------------------------------------------------    
   85 //
   86 function 3dtri_centerOfGravityCoord (Acord,Bcord,Ccord) = [
   87         (Acord[0]+Bcord[0]+Ccord[0])/3,(Acord[1]+Bcord[1]+Ccord[1])/3,0];
   88 //
   89 //
   90 // ===========================================
   91 //
   92 //  FUNCTION:       3dtri_centerOfcircumcircle
   93 //  DESCRIPTION:
   94 //      Enter triangle  A,B,C - corner coordinates to get the
   95 //      circum circle coordinates. It is assumed
   96 //      the triangle is parallel to the  X-Y plane. The function 
   97 //      returns always zero for the z-coordinate
   98 //  PARAMETER:
   99 //      Acord   : [x,y,z]   Coordinates of vertex A
  100 //      Bcord   : [x,y,z]   Coordinates of vertex B
  101 //      Ccord   : [x,y,z]   Coordinates of vertex C
  102 //  RETURNS:
  103 //      cc  : [x,y,0]   Circumcircle center
  104 //
  105 //  COMMENT:
  106 //      vertices = 3dtri_sides2coord (3,4,5);
  107 //      cc = 3dtri_centerOfcircumcircle (vertices[0],vertices[1],vertices[2]);
  108 // -------------------------------------------------------------------------------------    
  109 //
  110 function 3dtri_centerOfcircumcircle (Acord,Bcord,Ccord) = 
  111         [0.5*Bcord[0],
  112          0.5*((pow(Ccord[1],2)+pow(Ccord[0],2)-Bcord[0]*Ccord[0])/Ccord[1]),
  113          0];
  114 //
  115 // 
  116 //
  117 // ===========================================
  118 //
  119 //  FUNCTION:       3dtri_radiusOfcircumcircle
  120 //  DESCRIPTION:
  121 //       Provides the triangle's radius from circumcircle  to the vertices.
  122 //      It is assumed the triangle is parallel to the  X-Y plane. The function 
  123 //      returns always zero for the z-coordinate
  124 //  PARAMETER:
  125 //      Vcord   :   [x,y,z]     Coordinates of a vertex A or B,C
  126 //      CCcord : [x,y,z]    Coordinates of circumcircle
  127 //      r   : Radius at vertices if round corner triangle used,
  128 //            else enter "0"
  129 //  RETURNS:
  130 //      cr  : Circumcircle radius
  131 //
  132 //  COMMENT:  Calculate circumcircle radius of trinagle with round vertices having
  133 //          radius R = 2
  134 //      vertices = 3dtri_sides2coord (3,4,5);
  135 //      cc = 3dtri_centerOfcircumcircle (vertices[0],vertices[1],vertices[2]);
  136 //      cr = 3dtri_radiusOfcircumcircle (vertices[0],cc,2); 
  137 // -------------------------------------------------------------------------------------    
  138 //
  139 function 3dtri_radiusOfcircumcircle (Vcord,CCcord,R) = 
  140         sqrt(pow(CCcord[0]-Vcord[0],2)+pow(CCcord[1]-Vcord[1],2))+ R;
  141 //
  142 //
  143 //
  144 // ===========================================
  145 //
  146 //  FUNCTION:       3dtri_radiusOfIn_circle
  147 //  DESCRIPTION:
  148 //      Enter triangle  A,B,C - corner coordinates to get the
  149 //      in-circle radius. It is assumed the triangle is parallel to the  
  150 //      X-Y plane. The function always returns zero for the z-coordinate. 
  151 //      Formula used for inner circle radius: r = 2A /(a+b+c)
  152 //  PARAMETER:
  153 //      Acord   : [x,y,z]   Coordinates of vertex A
  154 //      Bcord   : [x,y,z]   Coordinates of vertex B
  155 //      Ccord   : [x,y,z]   Coordinates of vertex C
  156 //      
  157 //  RETURNS:
  158 //      ir  : real      radius of in-circle
  159 //
  160 //  COMMENT:
  161 //      vertices = 3dtri_sides2coord (3,4,5);
  162 //      ir = 3dtri_radiusOfIn_circle (vertices[0],vertices[1],vertices[2]);
  163 // -------------------------------------------------------------------------------------    
  164 //
  165 function 3dtri_radiusOfIn_circle (Acord,Bcord,Ccord) = 
  166     Bcord[0]*Ccord[1]/(Bcord[0]+sqrt(pow(Ccord[0]-Bcord[0],2)+pow(Ccord[1],2))+
  167     sqrt(pow(Ccord[0],2)+pow(Ccord[1],2)));
  168 //
  169 // 
  170 //
  171 // ===========================================
  172 //
  173 //  FUNCTION:       3dtri_centerOfIn_circle
  174 //  DESCRIPTION:
  175 //      Enter triangle  A,B,C - corner coordinates to get the
  176 //      in-circle coordinates. It is assumed
  177 //      the triangle is parallel to the  X-Y plane. The function 
  178 //      returns always zero for the z-coordinate
  179 //  PARAMETER:
  180 //      Acord   : [x,y,z]   Coordinates of vertex A
  181 //      Bcord   : [x,y,z]   Coordinates of vertex B
  182 //      Ccord   : [x,y,z]   Coordinates of vertex C
  183 //      r   : real      radius of in-circle
  184 //  RETURNS:
  185 //      ic  : [x,y,0]   In-circle center co-ordinates
  186 //
  187 //  COMMENT:
  188 //      vertices = 3dtri_sides2coord (3,4,5);
  189 //      ir = 3dtri_radiusOfIn_circle (vertices[0],vertices[1],vertices[2]);
  190 //      ic = 3dtri_centerOfIn_circle (vertices[0],vertices[1],vertices[2],ir);
  191 // -------------------------------------------------------------------------------------    
  192 //
  193 function 3dtri_centerOfIn_circle (Acord,Bcord,Ccord,r) = 
  194         [(Bcord[0]+sqrt(pow(Ccord[0]-Bcord[0],2)+pow(Ccord[1],2))+
  195     sqrt(pow(Ccord[0],2)+pow(Ccord[1],2)))/2-sqrt(pow(Ccord[0]-Bcord[0],2)+pow(Ccord[1],2)),r,0];
  196 //
  197 //
  198 // ============================================
  199 //
  200 //  MODULE:         3dtri_draw
  201 //  DESCRIPTION:
  202 //       Draw a standard solid triangle with A,B,C - vertices specified by its 
  203 //      co-ordinates and height "h", as given by the  input parameters. 
  204 //  PARAMETER:
  205 //      Acord   : [x,y,z]   Coordinates of vertex A
  206 //      Bcord   : [x,y,z]   Coordinates of vertex B
  207 //      Ccord   : [x,y,z]   Coordinates of vertex C
  208 //      h   : real      Hight of the triangle
  209 //  RETURNS:
  210 //      none
  211 //
  212 //  COMMENT:
  213 //      You might use the result from function 3dtri_sides2coord
  214 //      to call module 3dtri_draw ( vertices[0],vertices[1],vertices[2], h) 
  215 // -------------------------------------------------------------------------------------    
  216 //
  217 module 3dtri_draw ( Acord, Bcord, Ccord, h) {
  218 polyhedron (points=[Acord,Bcord,Ccord,
  219                     Acord+[0,0,h],Bcord+[0,0,h],Ccord+[0,0,h]],
  220             triangles=[ [0,1,2],[0,2,3],[3,2,5],
  221                     [3,5,4],[1,5,2],[4,5,1],
  222                     [4,1,0],[0,3,4]]);
  223 
  224 };
  225 //
  226 //
  227 // ==============================================
  228 //
  229 //  MODULE:         3dtri_rnd_draw
  230 //  DESCRIPTION:
  231 //      Draw a round corner triangle with A,B,C - vertices specified by its 
  232 //      co-ordinates, height h and round vertices having radius "r".
  233 //      As specified by the input parameters.
  234 //      Please note, the tringles side lenght gets extended by "2 * r",
  235 //      and the vertices coordinates define the centers of the 
  236 //      circles with radius "r". 
  237 //  PARAMETER:
  238 //      Acord   : [x,y,z]   Coordinates of vertex A
  239 //      Bcord   : [x,y,z]   Coordinates of vertex B
  240 //      Ccord   : [x,y,z]   Coordinates of vertex C
  241 //      h   : real      Hight of the triangle
  242 //      r   : real      Radius from vertices coordinates
  243 //  RETURNS:
  244 //      none
  245 //
  246 //  COMMENT:
  247 //      You might use the result from function 3dtri_sides2coord
  248 //      to call module 3dtri_rnd_draw ( vertices[0],vertices[1],vertices[2], h, r) 
  249 // -------------------------------------------------------------------------------------    
  250 //
  251 module 3dtri_rnd_draw ( Acord, Bcord, Ccord, h, r) {
  252 Avect=Ccord-Bcord;  // vector pointing from vertex B to vertex C
  253 p0=Acord + [0,-r,0];
  254 p1=Bcord + [0,-r,0];
  255 p2=Bcord + [r*Avect[1]/sqrt(pow(Avect[0],2)+pow(Avect[1],2)), 
  256                     -r*Avect[0]/sqrt(pow(Avect[0],2)+pow(Avect[1],2)) ,0];
  257 p3=Ccord + [r*Avect[1]/sqrt(pow(Avect[0],2)+pow(Avect[1],2)), 
  258                      -r*Avect[0]/sqrt(pow(Avect[0],2)+pow(Avect[1],2)) ,0];
  259 p4=Ccord +[- r*Ccord[1]/sqrt(pow(Ccord[0],2)+pow(Ccord[1],2)), 
  260                      r*Ccord[0]/sqrt(pow(Ccord[0],2)+pow(Ccord[1],2)) ,0];
  261 p5=Acord + [- r*Ccord[1]/sqrt(pow(Ccord[0],2)+pow(Ccord[1],2)), 
  262                      r*Ccord[0]/sqrt(pow(Ccord[0],2)+pow(Ccord[1],2)) ,0];
  263 bottom_triangles = [[0,1,2],[0,2,3],[0,3,4],[0,4,5]];
  264 c_side_triangles = [[7,1,0],[0,6,7]];
  265 a_side_triangles = [[2,8,3],[8,9,3]];
  266 b_side_triangles = [[4,10,5],[10,11,5]];
  267 A_edge_triangles = [[0,5,11],[0,11,6]];
  268 B_edge_triangles = [[1,7,2],[2,7,8]];
  269 C_edge_triangles = [[3,9,4],[9,10,4]];
  270 top_triangles = [[11,7,6],[11,8,7],[11,10,8],[8,10,9]];
  271 union () { 
  272     polyhedron (points=[p0,p1,p2,p3,p4,p5,
  273                     p0+[0,0,h],p1+[0,0,h],p2+[0,0,h],p3+[0,0,h],p4+[0,0,h],p5+[0,0,h]],
  274             triangles=[ bottom_triangles[0],bottom_triangles[1],bottom_triangles[2],bottom_triangles[3],
  275                         A_edge_triangles[0],A_edge_triangles[1],
  276                         c_side_triangles[0],c_side_triangles[1],
  277                         B_edge_triangles[0],B_edge_triangles[1],
  278                         a_side_triangles[0],a_side_triangles[1],
  279                         C_edge_triangles[0],C_edge_triangles[1],
  280                         b_side_triangles[0],b_side_triangles[1],
  281                         top_triangles[0],top_triangles[1],top_triangles[2],top_triangles[3]]);
  282     translate(Acord) cylinder(r1=r,r2=r,h=h,center=false);
  283     translate(Bcord) cylinder(r1=r,r2=r,h=h,center=false);
  284     translate(Ccord) cylinder(r1=r,r2=r,h=h,center=false);
  285 };
  286 }
  287 //
  288 // ==============================================
  289 //
  290 // Demo Application - copy into new file and uncomment or uncomment here but
  291 // without uncommenting the use <...> statement, then press F6 - Key 
  292 //
  293 // use <MCAD/3d_triangle.scad>;
  294 //$fn=50;
  295 //  h =4;
  296 //  r=2;
  297 //  echo ("Draws a right angle triangle with its circumcircle and in-circle");
  298 //  echo ("The calculated co-ordinates and radius are show in this console window");
  299 //  echo ("Geometry rules for a right angle triangle say, that the circumcircle is the");
  300 //  echo ("Thales Circle which center must be in the middle of the triangle's c - side");
  301 //  echo ("===========================================");
  302 //  vertices  =  3dtri_sides2coord (30,40,50);
  303 //  echo("A = ",vertices[0],"  B = ",vertices[1],"  C = ",vertices[2]);
  304 //  cg = 3dtri_centerOfGravityCoord (vertices[0],vertices[1],vertices[2]);
  305 //  echo (" Center of gravity = ",cg);
  306 //  cc = 3dtri_centerOfcircumcircle (vertices[0],vertices[1],vertices[2]);
  307 //  echo (" Center of circumcircle = ",cc);
  308 //  cr = 3dtri_radiusOfcircumcircle (vertices[0],cc,r); 
  309 //  echo(" Radius of circumcircle ",cr);
  310 //  ir = 3dtri_radiusOfIn_circle (vertices[0],vertices[1],vertices[2]);
  311 //  echo (" Radius of in-circle = ",ir);
  312 //  ic = 3dtri_centerOfIn_circle (vertices[0],vertices[1],vertices[2],ir);
  313 //  echo (" Center of in-circle = ",ic);
  314 //   translate(cc+[0,0,5*h/2]) difference () {
  315 //          cylinder (h=5*h,r1=cr+4,r2=cr+4,center=true);
  316 //          cylinder (h=6*h,r1=cr,r2=cr,center=true);}
  317 //  difference () {
  318 //      union () {
  319 //           difference () {
  320 //          3dtri_rnd_draw (vertices[0], vertices[1], vertices[2],5*h,r);
  321 //          scale([0.8,0.8,1]) translate([6,2,4*h])  3dtri_rnd_draw (vertices[0], vertices[1], vertices[2],5*h,r);
  322 //           }
  323 //           translate (ic+[0,0,5*h]) cylinder(h=10*h,r1=ir+r,r2=ir+r,center=true);
  324 //      }
  325 //      translate (ic+[0,0,5*h]) cylinder(h=12*h,r1=0.5*ir,r2=0.5*ir,center=true);
  326 //  }
  327