Repository: mikedh/trimesh

Issue:
pixel out of range in [scene.camera_rays()]
```
scene.camera.resolution = [512, 512]
origins, vectors, pixels = scene.camera_rays()
print(pixels[:3])
```
**output**
```
[[  0 512]
 [  0 510]
 [  0 509]]
```

the **512** should be **511**

I think that's because in `cameras.ray_pixel_coords` method:

```
    pixels = util.grid_linspace(
        bounds=[[0, res[1]], [res[0], 0]],
        count=res).astype(np.int64)
```
should change to
`bounds=[[0, res[1]-1], [res[0]-1, 0]]`

But I am not sure it's consistent with other code somewhere I don't know

Discussion:

Patch:
diff --git a/trimesh/base.py b/trimesh/base.py
index 06fc753c..7a6aaae8 100644
--- a/trimesh/base.py
+++ b/trimesh/base.py
@@ -1499,10 +1499,8 @@ class Trimesh(Geometry):
         >>> mesh.vertex_neighbors[0]
         [1, 2, 3, 4]
         """
-        graph = self.vertex_adjacency_graph
-        neighbors = [list(graph.neighbors(i)) for
-                     i in range(len(self.vertices))]
-        return np.array(neighbors)
+        return graph.neighbors(
+            edges=self.edges_unique, max_index=len(self.vertices))
 
     @caching.cache_decorator
     def is_winding_consistent(self):
diff --git a/trimesh/graph.py b/trimesh/graph.py
index efad2462..a93c087a 100644
--- a/trimesh/graph.py
+++ b/trimesh/graph.py
@@ -727,6 +727,40 @@ def edges_to_coo(edges, count=None, data=None):
     return matrix
 
 
+def neighbors(edges, max_index=None, directed=False):
+    """
+    Find the neigbors for each node in an edgelist graph.
+
+    TODO : re-write this with sparse matrix operations
+
+    Parameters
+    ------------
+    edges : (n, 2) int
+      Connected nodes
+    directed : bool
+      If True, only connect edges in one direction
+
+    Returns
+    ---------
+    neighbors : sequence
+      Vertex index corresponds to set of other vertex indices
+    """
+    neighbors = collections.defaultdict(set)
+    if directed:
+        [neighbors[edge[0]].add(edge[1])
+         for edge in edges]
+    else:
+        [(neighbors[edge[0]].add(edge[1]),
+          neighbors[edge[1]].add(edge[0]))
+         for edge in edges]
+
+    if max_index is None:
+        max_index = edges.max() + 1
+    array = [list(neighbors[i]) for i in range(max_index)]
+
+    return array
+
+
 def smoothed(mesh, angle=None, facet_minarea=15):
     """
     Return a non- watertight version of the mesh which
diff --git a/trimesh/scene/cameras.py b/trimesh/scene/cameras.py
index 517a04de..0c7d24d2 100644
--- a/trimesh/scene/cameras.py
+++ b/trimesh/scene/cameras.py
@@ -399,7 +399,7 @@ def ray_pixel_coords(camera):
 
     # create a matching array of pixel indexes for the rays
     pixels = util.grid_linspace(
-        bounds=[[0, res[1]], [res[0], 0]],
+        bounds=[[0, res[1] - 1], [res[0] - 1, 0]],
         count=res).astype(np.int64)
     assert xy.shape == pixels.shape
 
diff --git a/trimesh/scene/scene.py b/trimesh/scene/scene.py
index ece0440b..2a33d4bf 100644
--- a/trimesh/scene/scene.py
+++ b/trimesh/scene/scene.py
@@ -999,7 +999,7 @@ class Scene(Geometry):
         scale_3D = np.eye(4) * scale
 
         # preallocate transforms and geometries
-        nodes = self.graph.nodes_geometry
+        nodes = np.array(self.graph.nodes_geometry)
         transforms = np.zeros((len(nodes), 4, 4))
         geometries = [None] * len(nodes)
 
@@ -1027,7 +1027,7 @@ class Scene(Geometry):
                 # otherwise apply the full transform
                 result.geometry[geometry].apply_transform(new_geom)
 
-            for node, T in zip(self.graph.nodes_geometry[group],
+            for node, T in zip(nodes[group],
                                transforms[group]):
                 # generate the new transforms
                 transform = util.multi_dot(
diff --git a/trimesh/scene/transforms.py b/trimesh/scene/transforms.py
index e9e85dd9..d739c48a 100644
--- a/trimesh/scene/transforms.py
+++ b/trimesh/scene/transforms.py
@@ -263,7 +263,7 @@ class TransformForest(object):
         nodes : (n,) array
           All node names
         """
-        nodes = np.array(list(self.transforms.nodes()))
+        nodes = list(self.transforms.nodes())
         return nodes
 
     @caching.cache_decorator
@@ -276,10 +276,9 @@ class TransformForest(object):
         nodes_geometry : (m,) array
           Node names which have geometry associated
         """
-        nodes = np.array(
-            [n for n, attr in self.transforms.nodes(data=True)
-             if 'geometry' in attr])
-
+        nodes = [n for n, attr in
+                 self.transforms.nodes(data=True)
+                 if 'geometry' in attr]
         return nodes
 
     @caching.cache_decorator
diff --git a/trimesh/version.py b/trimesh/version.py
index 3994639a..29dbcd28 100644
--- a/trimesh/version.py
+++ b/trimesh/version.py
@@ -1,1 +1,1 @@
-__version__ = '3.6.26'
+__version__ = '3.6.29'


Test patch:
diff --git a/tests/generic.py b/tests/generic.py
index e3861220..f7c53e3b 100644
--- a/tests/generic.py
+++ b/tests/generic.py
@@ -71,7 +71,7 @@ except ImportError as E:
     has_path = False
 
 try:
-    from scipy import spatial, csgraph
+    from scipy import spatial, sparse
 except BaseException as E:
     if all_dep:
         raise E
diff --git a/tests/test_camera.py b/tests/test_camera.py
index f37c9a5f..69f45815 100644
--- a/tests/test_camera.py
+++ b/tests/test_camera.py
@@ -123,6 +123,18 @@ class CameraTests(g.unittest.TestCase):
         g.trimesh.scene.cameras.look_at(points, fov, center=points[0])
         g.trimesh.scene.cameras.look_at(points, fov, distance=1)
 
+    def test_ray_index(self):
+        # make sure to_rays is giving valid indexes
+        s = g.trimesh.scene.Scene()
+        res = g.np.array([512, 512])
+        for i in range(0, 1000, 79):
+            current = res + i
+            s.camera.resolution = current
+            # get ray index of camera
+            rid = s.camera.to_rays()[1]
+            assert all(rid.min(axis=0) == 0)
+            assert all(rid.max(axis=0) == current - 1)
+
 
 if __name__ == '__main__':
     g.trimesh.util.attach_to_log()