Refast extension for vtk
Project description
refast-vtk-js
3D visualization extension for Refast powered by react-vtk-js.
A Refast extension that provides VTK.js components for interactive 3D visualization in web applications.
Features
- View Components:
View,MultiViewRootfor creating 3D scenes - Representations:
GeometryRepresentation,VolumeRepresentation,SliceRepresentationfor different visualization modes - Data Components:
PolyData,ImageData,DataArrayfor defining geometry and data - Algorithms:
Algorithm,Readerfor data processing and file loading - Interaction:
Pickingfor mouse-based selection - Data Sharing:
ShareDataSetRoot,RegisterDataSet,UseDataSetfor efficient data reuse
Installation
pip install refast-vtk-js
Usage
Basic 3D Triangle
from fastapi import FastAPI
from refast import RefastApp, Context
from refast.components import Container
from refast_vtk_js import View, GeometryRepresentation, PolyData
ui = RefastApp(title="VTK.js Demo")
@ui.page("/")
def home(ctx: Context):
return Container(
children=[
View(
background=[0.2, 0.3, 0.4],
style={"width": "100%", "height": "400px"},
children=[
GeometryRepresentation(
property={"color": [1, 0, 0]},
children=[
PolyData(
points=[0, 0, 0, 1, 0, 0, 0.5, 1, 0],
connectivity="triangles"
)
]
)
]
)
]
)
app = FastAPI()
app.include_router(ui.router)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Using VTK Algorithms (Cone, Sphere, etc.)
from refast_vtk_js import View, GeometryRepresentation, Algorithm
View(
style={"width": "100%", "height": "400px"},
children=[
GeometryRepresentation(
children=[
Algorithm(
vtk_class="vtkConeSource",
state={
"height": 2.0,
"radius": 0.5,
"resolution": 60
}
)
]
)
]
)
Volume Rendering
from refast_vtk_js import View, VolumeRepresentation, ImageData, PointData, DataArray
# Create a 10x10x10 volume with scalar data
dimensions = [10, 10, 10]
values = [i / 1000 for i in range(10 * 10 * 10)] # Scalar values
View(
style={"width": "100%", "height": "400px"},
children=[
VolumeRepresentation(
color_map_preset="Cool to Warm",
children=[
ImageData(
dimensions=dimensions,
spacing=[1, 1, 1],
origin=[0, 0, 0],
children=[
PointData(
children=[
DataArray(
registration="setScalars",
values=values,
number_of_components=1
)
]
)
]
)
]
)
]
)
Loading 3D Models from URL
from refast_vtk_js import View, GeometryRepresentation, Reader
View(
style={"width": "100%", "height": "400px"},
children=[
GeometryRepresentation(
children=[
Reader(
vtk_class="vtkOBJReader",
url="https://example.com/model.obj"
)
]
)
]
)
Interactive Picking
from refast_vtk_js import View, GeometryRepresentation, PolyData, Picking
async def handle_click(ctx):
selection = ctx.event_data.get("selection", {})
world_pos = selection.get("worldPosition")
print(f"Clicked at world position: {world_pos}")
View(
style={"width": "100%", "height": "400px"},
children=[
Picking(
on_click=ctx.callback(handle_click),
pointer_size=5
),
GeometryRepresentation(
children=[
PolyData(
points=[0, 0, 0, 1, 0, 0, 0.5, 1, 0],
connectivity="triangles"
)
]
)
]
)
Sharing Data Between Views
from refast_vtk_js import (
ShareDataSetRoot, RegisterDataSet, UseDataSet,
View, GeometryRepresentation, PolyData
)
ShareDataSetRoot(
children=[
# Register the shared data
RegisterDataSet(
dataset_id="shared-mesh",
children=[
PolyData(
points=[0, 0, 0, 1, 0, 0, 0.5, 1, 0],
connectivity="triangles"
)
]
),
# First view
View(
style={"width": "50%", "height": "300px"},
children=[
GeometryRepresentation(
property={"color": [1, 0, 0]},
children=[UseDataSet(dataset_id="shared-mesh")]
)
]
),
# Second view using same data
View(
style={"width": "50%", "height": "300px"},
children=[
GeometryRepresentation(
property={"color": [0, 0, 1]},
children=[UseDataSet(dataset_id="shared-mesh")]
)
]
)
]
)
Available Components
View Components
View- Main 3D view containerMultiViewRoot- Container for multiple synchronized views
Representation Components
GeometryRepresentation- For surfaces, meshes, point cloudsGeometry2DRepresentation- For 2D overlaysVolumeRepresentation- For volume renderingSliceRepresentation- For 2D slices of 3D data
Dataset Components
PolyData- Polygonal geometry (points, lines, triangles)ImageData- Regular 3D grids (volumes)Dataset- Generic VTK dataset wrapper
Data Array Components
DataArray- Attach scalar/vector dataPointData- Container for point-associated arraysCellData- Container for cell-associated arraysFieldData- Container for field data
Algorithm & Reader Components
Algorithm- VTK filters and sourcesReader- File readers for OBJ, STL, VTK, etc.
Interaction Components
Picking- Mouse-based selectionVolumeController- UI for volume transfer functions
Data Sharing Components
ShareDataSetRoot- Root for data sharingRegisterDataSet- Register shared dataUseDataSet- Use registered data
Manual Registration
If auto-discovery doesn't work, you can manually register the extension:
from refast_vtk_js import VtkExtension
ui = RefastApp(
title="VTK Demo",
extensions=[VtkExtension()]
)
Development
Building the Frontend
cd frontend
npm install
npm run build
Watch Mode
npm run dev
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
refast_vtk_js-0.1.0.tar.gz
(55.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
refast_vtk_js-0.1.0-py3-none-any.whl
(346.1 kB
view details)
File details
Details for the file refast_vtk_js-0.1.0.tar.gz.
File metadata
- Download URL: refast_vtk_js-0.1.0.tar.gz
- Upload date:
- Size: 55.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
322958710862afadb26409e6751d1f222d117ff4b3fe61733992b5e963d1843c
|
|
| MD5 |
5c60bd21955cbb9f03cb0b21c1cc78e3
|
|
| BLAKE2b-256 |
e0a6cf369aa0ca897dc174f65da8eb898ca594af2f4f412530fd6ed3a2d6d48f
|
File details
Details for the file refast_vtk_js-0.1.0-py3-none-any.whl.
File metadata
- Download URL: refast_vtk_js-0.1.0-py3-none-any.whl
- Upload date:
- Size: 346.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c053e762e2e5a0ff2760b5ba063bb5f72d4adce08d05e6323f828fd71c2d8dd9
|
|
| MD5 |
5d2175e592e3ab31f24f6143008c8b64
|
|
| BLAKE2b-256 |
89c9c6ad4ba18bd03c3832bb7add49e131898420d050b7569240f6fdfe3a6284
|