 Hey there, welcome back to this series on writing CAD Software from scratch in the last video. We came up with a routine that could convert any generic polygonal mesh into an STL file that you could you know 3d print or send off for Fabrication or whatever basically that's just the groundwork for Everything else we're going to do in this series. So Let me explain exactly what that means so I mean it's a roadmap here Whatever you do in CAD pretty much has to go through this polygonal mesh state You know whether you're working in solid works or free CAD behind the scenes It's making a polygonal mesh whenever you want to render something Polygonal mesh whenever you make an STL file like we did before Polygonal mesh you want to figure out the geometry properties or mass properties want to find the CG I Think probably is using a polygonal mesh to do so You want some other output file you want I just you want whatever else? Polygonal mesh want to do some FEA you want to do some CFD again polygonal mesh It's like the the cornerstone for everything that you want to do in CAD and that's why it's going to be the focus of our of our series pretty much So as I said before we did this STL file last time we made a routine that does that not very pretty if you look we actually used a An ASCII STL, which basically you have to write down solid name and then face normal all this stuff in words Not very efficient It's not too bad really, but it doesn't scale very well There's a better way to encode it and that's a binary STL and that's what we will do in this video. So a Binary STL basically starts off with a header which 80 bytes that apparently you can ignore it You can leave it all zeros Which is awesome, then you have to put a four byte value of how many triangles there are in your mesh So I guess for us, I think there were there was 14 triangles in this mesh if I recall Then for each triangle you have to give again the normal vector 12 bytes three vertices each just 12 bytes and then This thing called the attribute byte count. I'm not sure what that's for really It just brings up the total number of bytes to 50 and I think you can leave it at zero Yeah, it should be zero so Pretty simple to encode. We're not gonna have to do too much one key difference from last time though Is that everything has to be a Sort of like a four byte value. So you see there's there's three components for these these values here and They're all 12 bytes each that means everything has to be four byte I if I recall I think we use doubles in our In our code before everything here is a double. So Let's just go over this again just to refresh we had a an array here for the nodes This is basically the the coordinates in 3d space of every point on that shape. We just showed so 0000 100 these are the coordinates in 3d space for every vertex Let me have this into triangles array that shows every element So the first element has vertices zero one and five second triangle has vertices zero five and four and so on And We basically had a routine here that just writes an STL file So we'll do another one, but instead of being an ASCII will be a binary STL the other one out and You know what just to be consistent. I'll change everything else to be a float There's no reason to use a double anyway, we're not like that precise. It's just a stupid test Anyway, so Let's make everything here a float Everything here a float who cares if we need to or not Okay, and then I think this okay. Good enough Now we'll just redo the same routine again before a binary STL so Won't this won't be too hard. I don't think we'll see maybe I'll mess it up. So Again, like we said before the first thing you have to do is write a header and then number of triangles so if you if you want to write something in In a binary, it's not f print f. It's just f right and then what you what you do is you hold on you basically You pass in a value here or I said an address of a value fold by the sort of number of Bites per element in that value and the number of values in that Array or whatever it would be And then you put location over your writing So to do that we have to define some some variables up front. So we'll make a header array So here an 80 byte header array. So it's very simple. Say care header size 80 equals zero all zeros And then the next thing here is number of triangles believe it or not We actually have a value for that in some triangles so we can just use the one from before But we will need another value this attribute by count is not going to change for every Triangle, so we'll just define that up front. We'll say care Attribute by size two Yep, that should be good and now to write something we'll say Right headers, but that's the Address of the first element in this header array Then I said there is one by per element in any elements. So I'm just like that. We'll have our header written next thing is To write the number of triangles so we'll pass in the address of That is a four-byte value just one into F just like that we finished the first 84 bytes of our Binary STL. So not too hard now. We can get into the you know meat of the actual function Not too different from before So just like before we have to calculate the normal vector like in the Wikipedia page so Use the same code as before float normal get normal, but instead of writing the values f print F we'll write them with a fright and Actually, it's much easier to output this because all you have to do is say F right Normal because there's a there's a array by itself called normal It's a real float. So everything is going to be Four bytes, there's three of them and then we're writing it to F. So not not too hard at all So at this point we've write it we've written the header the number of triangles We're in the normal vector for every triangle. I'm gonna talk about the vertices so we'll as before we'll loop over three vertices and instead of Writing them like this with f print F. We'll write them with F, right? I think we'll write As it says here nodes triangles ij again, there's four bytes per value three values per element and Kill this and now Last thing to do is write this attribute by count. So Instead of f print F We'll just do F right Attribute by One two plus F Honestly, that should just work out of the box. Let's let's give it a go. I guess so We have an STL. Let's see what it looks like Nice nice. I know what this means Awesome. Now, let's open it in this this tool. It's just refresh and again. We have our STL There it is just as before So it works and and now our STL is 784 Bytes, which by the way makes sense, right? I said there was 14 Triangles for each triangle those 50 bytes that's 700 plus 84 So we have exactly the number of bytes we expect now if we look Back to the previous one Yeah, so last video we had it 2.4 K File size Now it's only 780 so you can see this is much more efficient way To show data obviously for this particular model. It's nothing special. I mean, it's not huge Once you go to a very large model this difference will start to add up quite a bit so actually That's all I wanted to do in this video. I have another Video to output to upload today about rendering so I'll get on that That's it for this video. We've done a binary STL. I can kind of double check off this line and Yeah, see you next time