Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expression to extract vertex data from STL file exported by 3D software

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use regular expressions to extract vertex data in STL files derived from 3D software", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use regular expressions to extract vertex data in STL files derived from 3D software".

Let's first briefly introduce the regularization in matlab.

(not all, just what I'm going to use next)

Regexpi (String, pattern)% ignores case

This will return all the locations where pattern appears in String. If it is empty, there is no patern in String.

Match = regexpi (String, pattern, 'match')% ignore case

This will return all the pattern that appears in String, and the return value will be a cell

All the pattern that appears is placed in this cell as a separate element.

If there is no match to pattern, then match is empty.

Let's talk about this pattern.

If pattern = 'vertex', then you will find the exact same location as' vertex''in String.

That is: if pattern is a known string (there is only one possibility), then look for the known string in String

If pattern is an uncertain string (there are many possibilities), you'll have to define the pattern yourself.

\ + means the sign'+'.

\-means'- 'sign

[] represents a character set, and [\ +\ -] indicates that this position can be a'+ 'sign or a'-'sign (can only represent one character)

\ d represents a number

* the character before the'* 'sign can be repeated any number of times, and\ d* represents a number of any length.

All right, as long as you know all of the above, you can complete the pattern needed in this tweet.

So let's analyze the rules needed to filter out the vertex coordinates in the STL file:

The stl file is as follows:

1. Each vertex data starts with vertex (ignore spaces)

two。 So as long as there is' vertex', in this line, then there are vertex coordinates.

3. To determine whether 'vertex' exists in a line, you can use the following methods:

If ~ isempty (regexpi (line, 'vertex'))

4. So then analyze the string of the vertex coordinates.

The coordinate values in each direction are preceded by either a + sign or a-sign:

[\ +\]

The plus or minus sign is followed by a string of numbers:

\ d *

Then it will be the dot.

\.

And then there's a string of numbers.

\ d *

And then to'E'.

E

Then go to the + sign or the-sign.

[\ +\]

And then a string of numbers.

\ d *

5. Then the connection is:

'[\ +\ -]\ d*\.\ d* E [\ +\ -]\ dfarmers'

6. Obtain coordinate points through regexpi

Xyz_str_cell = regexpi (line,'[\ +\ -]\ d *\.\ d * E [\ +\ -]\ dlegs, 'match')

And then it's done:

Clc

Clear

File = fopen ('palne_stl.txt', 'r')

F = @ (c) str2num (c)

Vertexs = [];% three columns (x, y, z), n rows, each three rows as a group

While ~ feof (file)

Line = fgetl (file)

If ~ isempty (regexpi (line, 'vertex'))

Xyz_str_cell = regexpi (line,'[\ +\ -]\ d *\.\ d * E [\ +\ -]\ dlegs, 'match')

Xyz_cell = cellfun (f, xyz_str_cell, 'UniformOutput', false)

Xyz = cell2mat (xyz_cell)

Vertexs = [vertexs; xyz]

End

End

Fclose (file)

% len = length (vertexs) is definitely an integral multiple of 3, otherwise the file is incorrect

For I = 1: length (vertexs)

If ~ rem (I, 3)

Point1 = vertexs (iMur2,:)

Point2 = vertexs (iMur1,:)

Point3 = vertexs (I,:)

X = [point1 (1), point2 (1), point3 (1)]

Y = [point1 (2), point2 (2), point3 (2)]

Z = [point1 (3), point2 (3), point3 (3)]

Patch (x, y, z,'r')

End

End

Every element in xyz_str_cell is a numeric string, so you need to convert it with str2num, and to avoid loops, use cellfun.

After cellfun conversion, although from a string to a number, but the whole is still a cell, so use cell2mat conversion.

Fopen and fgetl can get one line in a file at a time.

So the vertex data is saved.

Then use patch to draw each piece, with three groups of vertices as one piece.

Finally, the whole thing is completed, that is, with the function of stl2matlab, the original model can be drawn directly at the same time.

You can modify the properties of patch to achieve the results you want. I didn't make any changes on it.

You can package this script into a function, give it a name, add it to the path, and then you can use it directly later.

Finally, the final model is attached:

The above is all the contents of the article "how to use regular expressions to extract vertex data from STL files exported by 3D software". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report