 Welcome back fifth video in this CAD from scratch series. At this point I'm pretty kind of stoked actually because we created a pretty cool bare minimum CAD tool so far. We can generate and encode a polygonal mesh like this one. We can render to the screen. We can rotate it in an ergonomic way, a user-friendly way and we can export this to an STL. So pretty much that covers all the basics of what CAD has to do right. Obviously you might want the ability to draw sketch and extrude and have boolean operations and lofts and fillets and stuff and we'll get to all those things in time but for the time being this is actually enough and I've done this kind of stuff for 10 years or actually more than that and I really enjoy generating parametric polygonal meshes for different parts and I've done that for a lot of different things and if you think back like 30, 40, 50 years this is kind of how scientific computing was done in terms of like you know finite elements and simulations and kinematics and stuff like that. These are the kind of models that were created. Nowadays we just use CAD with our eyes closed but this kind of these kind of algorithms that I'm going to talk about today are very important so what I wanted to do today is to make a much more complicated model than this one on the left. I wanted to make a clip like this just to show that it is possible to make a functional CAD model without any tools like completely from the code side. So that's the plan. So here's the clip. Basically it had a few parameters. We'll have L which will be the length of this flat section here. Let me from from here to here. We'll have what else? We'll have a thickness here. We'll call that W I guess with. We'll have a depth. We'll call that thickness. We'll have a radius on these little front pieces here. We'll have a gap in here. We'll call that G and what else do we need? 1, 2, 3, 4, 5. Oh I guess we need a parameter on this. These curved parts obviously in a polygonal mesh with polygons or an STL file even. You can't have a curve. No such thing as a curve so we'll have to encode these curves as a series of line segments you know. And so I'll say each of these arcs will have N line segments. Let's just jump right in and get started. At the bottom of our old code I got to put this in sub files at some point but for the time being let's get rid of everything in our main. Let's make a new function make clip and we'll pass in some parameters to that in a second. So for this make clip function we need to give the parameters. What kind of parameters do we need? Well all these floats so we'll say float t float w float let's do L first float g and then int n. That's six right? Yeah. And let's just give some values to that before we forget. Let's make it half a unit. We'll do inches here so point five inches thick. We'll do 0.1. We'll make it one inch long. We may have to scale it up later but that's the beauty of making a parametric model is that you can just change these parameters whenever you want. Make the radius here 0.1 we'll make the gap really small 25 thou and we will let's stick with 10 segments per curve for the time being. We can bump that up later. So now like before we need a couple of we want to create as before we want to create a float array of nodes which is a length num nodes 3 and we want to create a an int array of triangles which is what we had before length num triangles by 3. So we need to define those two variables so int num nodes equals something and then int num triangles equals something. So what are those two things? Well let's think right. So the number of nodes will be governed primarily by n in fact exclusively by n so let's say for this video in terms of what I'm going to show you I'm just going to show you how to do the outside of this model so like this face you know this face not I'm not going to do the front and the back I'm not going to do this face and the face on the back. I'll do it myself and I'll show you the result I don't want to waste too much time. So to do the outside of this model how many nodes will there be? Well if this is going to be n segments this will have n plus one nodes obviously even if you have you know three segments you have one two three four nodes so n plus one and it's not going to be any nodes on the flat portions but every curve will have n plus one so we have one two three four curves so on one of these faces on the front of the back we will have on the boundary four times n plus one nodes total and then if you count the front and the back so the nodes here as well as the nodes here or I should say now the nodes here and the nodes here you will have eight and plus one right two times four is eight so let's say that for number of nodes equals eight times n plus one number of triangles I think it's the same thing and let me explain why that is if you were to look at this from the the top right so this this face here let me just draw a little bit nicer head on you'll have a bunch of nodes on this side here right that's where the curved surfaces have a bunch of nodes here should have no nodes in the middle and so this middle face will just be two triangles like this draw straight line and then all of these nodes between the front and the back will be connected like this you'll have between each you know edge you'll have two triangles and so for that reason I think it's going to be the same number of nodes as triangles and another way of thinking about that is just that you know how many edges do you have on one surface while you have n edges per curve and then one edge per flat so that's going to be this four curves so four n plus four and then there's two triangles per edge so this is going to be times two so again we're in the same boat the number of triangles is eight times n plus one now comes the interesting part we have to make some other sub functions because I don't want to sit here all day quoting this up manually so we'll need a function that does first thing it function that makes a an arc in the x y plane so put this perspective I want to make first things first I want to draw one of these faces so let me change my color really quick I want to make this sort of set of nodes first and what is the set of nodes well this set of nodes is composed of an arc and then another arc and then another arc and then another arc then back so really it's just four calls to a function that draws an arc and we'll say that this plane here this is the x y plane and we'll put the x y with the origin of that right right here that should be an easy spot to put the origin so x y my god this is impossible x y so we need an arc in the x y plane we also need a function that gives us an extrude or maybe a close and extrude curve in the z direction basically once we have you know our front face or our back face whatever I want to go to extrude it by depth t didn't just copy and paste and then draw all the triangles between the two so that's going to be the second function here so the first function it's actually pretty simple I guess you want to return the next index because we want to call this function a few times for three different arcs so we'll see return the next index in node array what does that mean we'll we will have to pass in the notary obviously because we're going to populate this notary with this function so we'll pass in float nodes obviously this is just a you know a pointer here but we'll also just pass in an index because obviously we want to be able to put in multiple sets of curves so we have to know where in this array to to put those so we'll pass in and start index we will also need a bunch of parameters now so to define an arc is quite a quite a bit to define an arc right actually get rid of all this so an arc let me change color again and go a little bit bigger so an arc like this has a center right so we has x y and z it also has a theta start so if you if you count an angle right so angle starts at zero over here that goes counterclockwise around so it has a theta start so say theta zero and a theta end or theta one I guess also has a radius r and what else does it have well for our case we're going to have a bunch of line segments so we'll have n like increments around this arc basically so we'll pass in these values we'll say float x 0 float y 0 z 0 float theta 0 float theta 1 float r int and yakes okay and actually it's pretty simple right once I give you these parameters it's very easy first thing you need when you have these parameters is you need to calculate the the d theta the theta between each line segment right so we'll calculate that so we'll say float d theta equals obviously just a theta 1 minus theta 0 divided by the n the n which is guess well they do float here and now we just have to iterate through this is pretty straightforward we're going to iterate through n plus one nodes put them into this nodes array starting at the start index so say four int i equals zero i less than equal to n because n plus one i plus plus and I guess because we want to return the next index we will return start and we'll just increment that as we go so this is pretty straightforward obviously we're going to use trigonometry here so we need to define an angle so we'll say float angle equals but theta zero plus i times d theta right so however far we have ever far we are just add that many d-thetas and now we just literally populate the array so we'll say nodes start zero equals so what is the coordinate the x-coordinate of this first node here we do another color this first node actually any node is just going to be x zero plus r times cosine of the angle right very simple then the y-coordinate is just y zero plus r sine of the angle and then the z-coordinate because we're just gonna stay in the x-y plane we're gonna leave the z-coordinate at our z zero value and that should work I guess we'll find out oh no sorry we have to increment starts right so we'll just increment starts at the last last call of each loop so this should give us any given arc that we could ever imagine in the x-y plane now we want to be able to extrude this so let me kill these really quick so as an extrude let's go from the top down view again so this is this is the top of the model looking down once we go through and we create all the arcs right of the front service call this the front and this the back once we all the points on the front will have nodes like here here here here here and here and we'll need to extrude those the color they copy and paste those nodes literally just some distance t in the z direction right that's gonna be the first thing and then in the same function we're gonna have to create all these triangles here so all these triangles so basically if this is gonna be this is gonna be node zero and this is gonna be node one and this is gonna be node you know k where k is the number of nodes on each face front or back and this is gonna be k plus one right and we'll have to hit some relationship to figure out how this connect is it goes zero one k plus one as it go k k plus one zero we'll have to figure that out in this function so we'll say int what we want to call this function it extrude close curve that sounds like a cad closed curve and this one we'll have to pass in a couple things we have to pass in obviously the nodes array because we're gonna literally be copying and pasting nodes forward or backward in the z direction we have to pass in the triangles right because we're gonna be making bunch of triangles right over here so we'll say int triangles we'll need to pass in the number of nodes I guess k so we'll just say int nodes curve I don't know curve and then the obviously the depth we'll say int depth or int t whatever you want to do now for this function I think we need an I mean a count but we'll do that later literally we're just gonna iterate through these front nodes here from zero to k obviously this should not be k this should be a k plus one if we're gonna use k k plus one k plus two and this last one should be k obviously you have to go all the way around the model you can't just go from the top to the left right you have to go from you know here all the way around over and across and back and so this would be zero and this would be k right around the back but still that makes sense so we're gonna iterate over the number of nodes in the curve so I'll say for int I equals zero I less than num what I call num nodes curve there it probably I plus plus and in here we're just gonna literally copy and paste things forward so pretty much just what I just said so zero is gonna become you know just literally copying it forward that many points and actually if we're passing the number of nodes it I think it will be k because we started zero so yeah this actually be k minus one it's right the first time whatever you get the point so we'll say nodes num nodes curve plus I zero equals nodes I zero same thing for the y component and the z component however the z component as we just said we're extrading this so we'll say plus the depth the depth in the z direction now that will do the pretty much the easy part which is just copying the front nodes to the back copying the front nodes to the back and now we have to make the actual triangle connectivity so the triangle array so say triangles of okay we have to have a counter here so we'll say like that so let's define a counter we'll start with the ith node so what is the what triangles involve the ith the ith node so it's zero well we'll need probably one so we'll say triangles CT one so I should refresh your memory this triangles array encode encodes which nodes are in which triangle so there's three components zero one and two and ask which nodes are in which triangle so yeah just click this more closest k scuffed scuff video k k plus one and so this triangle zero you know one depending on how you want to draw it we'll either attach with k or k plus one or however you want to attach it so let's say it starts with k so our first triangle is zero one k so we'll say in this case it's not just zero it's I right it's I plus zero I plus one I plus k so say I I plus one I plus k and then the the next triangle will be very similar so it this triangle here is zero one and k the next triangle has one k plus one and k right or you guess you could even start with whatever you want so let's just say the first one is I know I plus k also we didn't define k it's actually num nodes curve num nodes it's such a scuff video I think I there's no audience num nodes curve plus plus one yeah that makes sense obviously we have to increment these counters for each triangle so we're adding two triangles per node so this will be what we do and then the thing is this will work for everything except let's return ct while we're at it this will not work for the very last one so as I said if this is going to be zero and this is going to be k minus one this you know equivalency you know this relationship is not going to make sense for the last couple of nodes so the last node let's let's think about this really quick the last two triangles so we have ct minus two and triangles ct minus one will have to be changed slightly let's think about this the ife one I plus one will have to be zero this one will have to be zero you have to take my word for this hard to explain these things visually you kind of just imagine going forward all the way around to this point and then the second one as opposed to being I num nodes curve it will be I know her plus one it will be num nodes curve I guess right is that right that seems right to me yeah that seems right to me we're replacing those two okay that's fine we'll see that works now I don't know we'll get lucky now let's actually call these functions in our our make clip routine so this is actually really easy let's define some some counters see ct both zero and now we're just going to say c equals what's it called arc node or no dark no dark we're gonna pass in those parameters let's just copy it down so I put them in the right spot okay thank God actually so it's um nodes see now here comes the tough part how do you want to count around this geometry let me kill all this garbage let's start this model here's the origin let's start right here so the first what's the first one we have to fill in we have to fill in the x zero y zero zero zero so it's going to be zero I'll fill these in and I'll get back this is taking me a few minutes to fill these in okay I put in all these no dark functions with the with the values of the parameters which would correspond to this kind of a traversal of the outside of the model you know I hope they're all right then I went ahead and I extruded it with the parameter we just talked about and then I put together this like a look look at look from like we had before a file name write stl and then a draw function here so this is what we had below here before I just re-made it up here on top and I did find a mistake I think yeah over here the variable called start should be not plural yeah so you can see that actually worked you can see we have a clip from some random I'm not sure what number I put in some random perspective but it looks like for some reason it has no depth let me take a look about why that would be okay I found what it was in our extrude closed curve I put it in for the depth obviously it's a float not a name so that didn't work but but yeah so now if I make and I compile or sorry I compiled and I run look what we have did look at this we have a clip this is the clip we just talked about we made this thing parametrically and it looks pretty sweet if you ask me so that's that but let me let me quickly code up the faces thing the front and the back face because right now this is not an STL it'll have you know an opening on the front and the back there's there's no triangles here so let me code those up I'll be back in a few minutes and yeah we'll see how it looks okay it's been like probably 10 minutes or so and I put in a bunch more more stuff to handle the front faces yeah also I had to change the number of nodes and triangles obviously to account for the front face I won't get into that if you care about the code it's in the description you can take a look yourself and this should work I did check it so if it doesn't work now that that would be surprising yeah so here's our here's our clip model with the front and the back face it's kind of like Santa's sleigh you know I don't know reminds me of like Santa Claus sleigh with this kind of front piece yeah you can see got nice triangles on the front face on the top on the side looks nice look over here look where this part is look at that looks sweet to you actually you know what hold on let me um let me change that end parameter from what we have 10 what do you think 50 it's 250 oof look at that look at that look at that now that looks nice we made this we made look at this didn't we made this that's pretty sweet and actually let's just check we have an stl file that's 40k let's see if this works there it is so you don't believe me it's real stl there it is there's your real stl file of that clip so yeah it's pretty nice obviously this is not the easiest way this probably took me like an hour to record this video so it definitely not the easiest way to go about like making a geometry this is just in free caps it'd be so easy just make a sketch with some constraints extra this it took you two minutes but you know from scratch things are a lot harder but in the next few videos we'll work on ways that we can kind of zoom out from this nitty gritty polygonal mesh stuff to more user-friendly like you know CSG and other other algorithms that you might want to use to make make geometries so that's the plan hope you enjoyed the video have a nice day