<commit_before>#+SETUPFILE: ../../src/setup.org

* Namespace: thi.ng.geom.types.utils

** Bounds

#+BEGIN_SRC clojure :noweb-ref bounds
  (defn bounding-rect
    [points]
    (let [[p size] (gu/bounding-rect points)]
      (if p (thi.ng.geom.types.Rect2. p size))))
  
  (defn bounding-box
    [points]
    (let [[p size] (gu/bounding-box points)]
      (if p (thi.ng.geom.types.AABB. p size))))
  
  (defn bounding-circle
    ([points]
       (bounding-circle (gu/centroid points) points))
    ([c r-or-points]
       (let [[c r] (gu/radial-bounds vec2 c r-or-points)]
         (thi.ng.geom.types.Circle2. c r))))
  
  (defn bounding-sphere
    ([points]
       (bounding-sphere (gu/centroid points) points))
    ([c r-or-points]
       (let [[c r] (gu/radial-bounds vec3 c r-or-points)]
         (thi.ng.geom.types.Sphere. c r))))
  
  (defn coll-bounds
    "Takes a seq of shape entities, calls g/bounds on each and returns
    union bounds. Does not support collections of mixed 2d/3d entities."
    [coll]
    (reduce g/union (map g/bounds coll)))
  
  (defn fit-all-into-bounds
    "Takes an AABB or rect and seq of shapes, proportionally scales and
    repositions all items to fit into given bounds. Returns lazyseq of
    transformed entities. Does not support collections of mixed 2D/3D
    entities. Use rects as target bounds for 2D colls."
    [bounds coll]
    (let [b (coll-bounds coll)
          s (reduce min (g/div (:size bounds) (:size b)))
          b' (g/center (g/scale b s) (g/centroid bounds))]
      (map
       #(-> %
            (g/center (g/unmap-point b' (g/map-point b (g/centroid %))))
            (g/scale-size s))
       coll)))
#+END_SRC

** Meshes

#+BEGIN_SRC clojure :noweb-ref meshes
  (defn into-mesh
    [mesh add-face mesh-or-faces]
    (reduce
     add-face mesh
     (if (satisfies? g/PFaceAccess mesh-or-faces)
       (g/faces mesh-or-faces)
       mesh-or-faces)))
  
  (defn map-mesh
    "Applies f to all faces of given mesh and adds resulting faces to
    new mesh. Thus f should return a seq of faces. Returns new mesh."
    [f mesh] (g/into (g/empty mesh) (mapcat f (g/faces mesh))))
  
  (defn transform-mesh
    [mesh add-face tx]
    (let [verts' (->> (g/vertices mesh)
                      (r/map (fn [v] [v (g/transform-vector tx v)]))
                      (into {}))]
      (reduce
       (fn [mesh f] (add-face mesh (replace verts' f)))
       (g/empty mesh) (g/faces mesh))))
#+END_SRC<commit_msg>bugfix transform-mesh for CLJS<commit_after>#+SETUPFILE: ../../src/setup.org

* Namespace: thi.ng.geom.types.utils

** Bounds

#+BEGIN_SRC clojure :noweb-ref bounds
  (defn bounding-rect
    [points]
    (let [[p size] (gu/bounding-rect points)]
      (if p (thi.ng.geom.types.Rect2. p size))))
  
  (defn bounding-box
    [points]
    (let [[p size] (gu/bounding-box points)]
      (if p (thi.ng.geom.types.AABB. p size))))
  
  (defn bounding-circle
    ([points]
       (bounding-circle (gu/centroid points) points))
    ([c r-or-points]
       (let [[c r] (gu/radial-bounds vec2 c r-or-points)]
         (thi.ng.geom.types.Circle2. c r))))
  
  (defn bounding-sphere
    ([points]
       (bounding-sphere (gu/centroid points) points))
    ([c r-or-points]
       (let [[c r] (gu/radial-bounds vec3 c r-or-points)]
         (thi.ng.geom.types.Sphere. c r))))
  
  (defn coll-bounds
    "Takes a seq of shape entities, calls g/bounds on each and returns
    union bounds. Does not support collections of mixed 2d/3d entities."
    [coll]
    (reduce g/union (map g/bounds coll)))
  
  (defn fit-all-into-bounds
    "Takes an AABB or rect and seq of shapes, proportionally scales and
    repositions all items to fit into given bounds. Returns lazyseq of
    transformed entities. Does not support collections of mixed 2D/3D
    entities. Use rects as target bounds for 2D colls."
    [bounds coll]
    (let [b (coll-bounds coll)
          s (reduce min (g/div (:size bounds) (:size b)))
          b' (g/center (g/scale b s) (g/centroid bounds))]
      (map
       #(-> %
            (g/center (g/unmap-point b' (g/map-point b (g/centroid %))))
            (g/scale-size s))
       coll)))
#+END_SRC

** Meshes

#+BEGIN_SRC clojure :noweb-ref meshes
  (defn into-mesh
    [mesh add-face mesh-or-faces]
    (reduce
     add-face mesh
     (if (satisfies? g/PFaceAccess mesh-or-faces)
       (g/faces mesh-or-faces)
       mesh-or-faces)))
  
  (defn map-mesh
    "Applies f to all faces of given mesh and adds resulting faces to
    new mesh. Thus f should return a seq of faces. Returns new mesh."
    [f mesh] (g/into (g/empty mesh) (mapcat f (g/faces mesh))))
  
  (defn transform-mesh
    [mesh add-face tx]
    (let [verts' (->> (g/vertices mesh)
                      ,#+cljs (seq)
                      (r/map (fn [v] [v (g/transform-vector tx v)]))
                      (into {}))]
      (reduce
       (fn [mesh f] (add-face mesh (replace verts' f)))
       (g/empty mesh) (g/faces mesh))))
#+END_SRC
{'fraction_non_alphanumeric': 0.1421381115361385, 'fraction_numerical': 0.0036239178578618887, 'mean_word_length': 2.2748846407382994, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2609444', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1754, 'n_words': 536}