IDirectedGraph | Matterport SDK
Usage of the SDK constitutes your agreement with the Matterport SDK Agreement. Email developers@matterport.com with any questions.

A directed, weighted graph data structure.

Type parameters

  • T

    The type of any user data associated with each vertex in the graph.

Hierarchy

  • IDirectedGraph

Index

Properties

Readonly edgeCount

edgeCount: number

The number of edges in the graph.

console.log(`the graph has ${graph.edgeCount} edges`);

Readonly edges

edges: IterableIterator<Graph.Edge<T>>

An iterable of all edges in the graph.

for (const edge of graph.edges) {
  console.log(`edge: ${edge.src.id} -> ${edge.dst.id}`);
}

Readonly vertexCount

vertexCount: number

The number of vertices in the graph.

console.log(`the graph has ${graph.vertexCount} vertices`);

Readonly vertices

vertices: IterableIterator<Graph.Vertex<T>>

An iterable of all vertices in the graph.

for (const vertex of graph.vertices) {
  console.log(`vertex: ${vertex.id}`);
}

Methods

addVertex

  • addVertex(...vertexData: T extends undefined | void ? VertexIdDescriptor[] : Array<VertexDescriptor<T>>): void
  • Add a vertex or set of vertices to the graph.

    // for vertices with undefined "data" (no data associated with each vertex)
    const undefGraph = mpSdk.Graph.createDirectedGraph<undefined>();
    graph.addVertex(
      { id: 'a' },
      { id: 'b' },
    );
    
    // for vertices with any other data
    const graph = mpSdk.Graph.createDirectedGraph<number>();
    graph.addVertex(
      { id: 'a', data: 1 },
      { id: 'b', data: 2 },
    );
    

    Parameters

    • Rest ...vertexData: T extends undefined | void ? VertexIdDescriptor[] : Array<VertexDescriptor<T>>

      A variable number of VertexDescriptors to use to create nodes in the graph.

    Returns void

commit

  • commit(): void
  • Trigger any attached observers if there were changes to this graph. If there are no changes to the graph, this is a no-op and no callbacks will be triggered.

    graph.commit();
    

    Returns void

dispose

  • dispose(): void
  • Remove all vertices and edges from the graph. Also call an optional onDispose provided at construction.

    graph.dispose();
    // graph.vertexCount === 0
    // graph.edgeCount === 0
    

    Returns void

edge

  • Get the edge that connects one vertex to another.

    const edge = graph.edge(a, b);
    if (edge) {
      console.log(`Found an edge from "${edge.src.id}" to "${edge.dst.id}" with weight ${edge.weight}`);
    }
    

    Parameters

    Returns Graph.Edge<T> | undefined

    If an edge from src to dst exists, returns the Edge. Otherwise, undefined.

hasEdge

  • Test if an edge from one vertex to another is present in the graph.

    if (graph.hasEdge(a, b)) {
      console.log('an edge from "a" to "b" was found');
    }
    

    Parameters

    Returns boolean

    If an edge from src to dst exists, returns true. Otherwise, false.

hasVertex

  • hasVertex(id: string): boolean
  • Test if a vertex associated with id is present in the graph.

    if (graph.hasVertex('a')) {
      console.log('a vertex with id "a" was found');
    }
    

    Parameters

    • id: string

      The vertex's id.

    Returns boolean

    If a vertex with id exists, returns true. Otherwise, false.

onEdgesChanged

  • Subscribe to edge changes.

    After this graph's edges have been updated using setEdge or removeEdge, the observer attached will have its onChanged function called when commit is called.

    graph.onEdgesChanged({
      onChanged() {
        console.log(`this graph's edges have changed`);
      }
    });
    

    Parameters

    Returns ISubscription

onVerticesChanged

  • Subscribe to vertex changes.

    After this graph's vertices have been updated using addVertex or removeVertex, the observer attached will have its onChanged function called when commit is called.

    graph.onVerticesChanged({
      onChanged() {
        console.log(`this graph's vertices have changed`);
      }
    });
    

    Parameters

    Returns ISubscription

removeEdge

  • removeEdge(...edges: Array<Graph.Edge<T>>): void
  • Remove an edge or a set of edges from the graph.

    graph.removeEdge(e1, e2);
    

    Parameters

    • Rest ...edges: Array<Graph.Edge<T>>

      A variable number of edges to remove.

    Returns void

removeVertex

  • Remove a vertex or a set of vertices from the graph.

    graph.removeVertex(a, b);
    

    Parameters

    • Rest ...vertices: Array<Graph.Vertex<T>>

      A variable number of vertices to remove.

    Returns void

setEdge

  • throws

    If src or dst is not in the graph.

    throws

    If weight is negative or not a number.

    Set the weight of an edge or set of edges between pairs of vertices representing the connections from src to dst.

    If an error is thrown, the graph is left untouched, no edges are added.

    graph.setEdge(
      { src: a, dst: b, weight: 5  },
      { src: b, dst: c, weight: 10 },
    );
    
    // update edge ab
    graph.setEdge(
      { src: a, dst: b, weight: 10 },
    );
    

    Parameters

    Returns void

vertex

  • Get the vertex associated with id.

    const a = graph.vertex('a');
    if (a) {
      console.log(`Found a vertex with id "${a.id}"`);
    }
    

    Parameters

    • id: string

      The vertex's id.

    Returns Graph.Vertex<T> | undefined

    If a vertex with id exists, returns the Vertex. Otherwise, undefined.

watch

  • Watch a collection and update the graph as the collection changes.

    Note: If you need a graph of enabled sweeps, use Sweep.createGraph instead of the code snippet below

    // create a graph of enabled sweeps
    const graph = mpSdk.createGraph();
    const sub = graph.watch({
      collection: mpSdk.Sweep.collection,
      isNeighborOf(sweepSrc, sweepDst) {
        return sweepSrc.data.neighbors.includes(sweepDst.id);
      },
      neighborsOf(sweepVertex) {
        return sweepVertex.data.neighbors.values();
      },
      weightBetween(sweepSrc, sweepDst) {
        const dx = sweepDst.data.position.x - sweepSrc.data.position.x;
        const dy = sweepDst.data.position.y - sweepSrc.data.position.y;
        const dz = sweepDst.data.position.z - sweepSrc.data.position.z;
        return Math.sqrt(dx ** 2 + dy ** 2 + dz ** 2);
      },
      shouldAdd(sweep) {
        return sweep.enabled;
      },
    });
    
    // some time later when the graph no longer needs updating
    sub.cancel();
    

    Parameters

    Returns ISubscription

    ISubscription