Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3872284
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:54:32+00:00 2026-05-19T21:54:32+00:00

I want to parse the text line from the Wavefront OBJ file . Currently

  • 0

I want to parse the text line from the Wavefront OBJ file. Currently I am interested in “V” and “F” types only.
My algorithm is as follows:

  1. check if line is not nil (otherwise step 2 would fail)
  2. drop comment after “#” and trim spaces
  3. drop prefix “v ” or “f “
  4. split string to the list of elements where each element
    1. is split to the list if it is symbol like |34/76/23|
    2. is converted from the list: I take one element only, the first by default
    3. or coerced to the given type if it is atomic number already.

Here is the code:

(defun parse-line (line prefix &key (type 'single-float))
  (declare (optimize (debug 3)))
  (labels ((rfs (what)
             (read-from-string (concatenate 'string "(" what ")")))
           (unpack (str &key (char #\/) (n 0))
             (let ((*readtable* (copy-readtable))) 
               (when char ;; we make the given char a delimiter (space)
                 (set-syntax-from-char char #\Space))
               (typecase str
                 ;; string -> list of possibly symbols.
                 ;; all elements are preserved by (map). nil's are dropped
                 (string (delete-if #'null
                                    (map 'list
                                         #'unpack
                                         (rfs str))))
                 ;; symbol -> list of values
                 (symbol (unpack (rfs (symbol-name str))))
                 ;; list -> value (only the requested one)
                 (list (unpack (nth n str)))
                 ;; value -> just coerce to type
                 (number (coerce str type))))))
    (and line
         (setf line (string-trim '(#\Space #\Tab)
                                 (subseq line 0 (position #\# line))))
         (< (length prefix) (length line))
         (string= line prefix :end1 (length prefix) :end2 (length prefix))
         (setf line (subseq line (length prefix)))
         (let ((value (unpack line :char nil))) 
           (case (length value)
               (3 value)
               (4 (values (subseq value 0 3) ;; split quad 0-1-2-3 on tri 0-1-2 + tri 0-2-3
                          (list (nth 0 value)
                                (nth 2 value)
                                (nth 3 value)))))))))

Step four (label “unpack”) is kind of recursive. It is one function and can call itself three times.

Anyway, this solution seems to be clunky.

My question is: how should one solve this task with shorter and clearer code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T21:54:33+00:00Added an answer on May 19, 2026 at 9:54 pm

    I would approach this in a more structured manner.

    You want to parse an obj file into some sort of data structure:

    (defun parse-obj-file (filespec)
      ;; todo
      )
    

    You need to think about how the data structure returned should look. For now, let us return a list of two lists, one of the vertices, one of the faces. The parser will go through each line, determine whether it is either a vertex or a face, and then collect it into the appropriate list:

    (defun parse-obj-file (filespec)
      (with-open-file (in-stream filespec
                                 :direction :input)
        (loop for line = (read-line in-stream nil)
              while line
              when (cl-ppcre:scan "^v " line)
              collect (parse-vertex line) into vertices
              when (cl-ppcre:scan "^f " line)
              collect (parse-face line) into faces
              finally (return (list vertices faces)))))
    

    I used the cl-ppcre library here, but you could also use mismatch or search. You will then need to define parse-vertex and parse-face, for which cl-ppcre:split should come in quite handy.

    It would perhaps also be useful to define classes for vertices and faces.

    Update: This is how I would approach vertices:

    (defclass vertex ()
      ((x :accessor x :initarg :x)
       (y :accessor y :initarg :y)
       (z :accessor z :initarg :z)
       (w :accessor w :initarg :w)))
    
    (defun parse-vertex (line)
      (destructuring-bind (label x y z &optional w)
          (cl-ppcre:split "\\s+" (remove-comment line))
        (declare (ignorable label))
        (make-instance 'vertex
                       :x (parse-number x)
                       :y (parse-number y)
                       :z (parse-number z)
                       :w (parse-number w))))
    

    Parse-number is from the parse-number library. It is better than using read.

    Update 2: (Sorry for making this a run-on story; I have to interlace some work.) A face consists of a list of face-points.

    (defclass face-point ()
      ((vertex-index :accessor vertex-index :initarg :vertex-index)
       (texture-coordinate :accessor texture-coordinate
                           :initarg :texture-coordinate)
       (normal :accessor normal :initarg :normal)))
    
    (defun parse-face (line)
      (destructuring-bind (label &rest face-points)
          (cl-ppcre:split "\\s+" (remove-comment line))
        (declare (ignorable label))
        (mapcar #'parse-face-point face-points)))
    
    (defun parse-face-point (string)
      (destructuring-bind (vertex-index &optional texture-coordinate normal)
          (cl-ppcre:split "/" string)
        (make-instance 'face-point
                       :vertex-index vertex-index
                       :texture-coordinate texture-coordinate
                       :normal normal)))
    

    Remove-comment simply throws away everything after the first #:

    (defun remove-comment (line)
      (subseq line 0 (position #\# line)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have opened an HTML file using file_get_contents('http://www.example.com/file.html') and want to parse the line
I want to parse a config file sorta thing, like so: [KEY:Value] [SUBKEY:SubValue] Now
I want to parse an Apache access.log file with a python program in a
A file name will be passed in from standard in. I want to open
I'm trying to write a program that reads text from external file (string string
I want to parse some HTML in order to find the values of some
I want to parse a web page in Groovy and extract all of the
I'm want to parse a custom string format that is persisting an object graphs
In my C++ program I want to parse a small piece of XML, insert
I work in VBA, and want to parse a string eg <PointN xsi:type='typens:PointN' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.