Skip to content

Instantly share code, notes, and snippets.

@somada141
somada141 / vtk_texture_sphere.py
Last active February 22, 2021 13:29
How to apply a texture to a sphere in VTK #python #visualization #vtk
#!/usr/bin/env python
##
# This example shows how to apply an vtkImageData texture to an sphere
# vtkPolyData object.
# Note: Input jpg file can be located in the VTKData repository.
#
# @author JBallesteros
##
@somada141
somada141 / vtk_glyphs_custom.py
Last active August 29, 2015 14:06
Create custom glyphs in VTK #python #visualization #vtk
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from vtk import *
# Let's create a surface with 3 points unconnected.
points = vtkPoints()
points.InsertNextPoint(1, 0, 0)
points.InsertNextPoint(0, 0, 0)
points.InsertNextPoint(0, 1, 0)
@somada141
somada141 / volume_rendering_vtk.py
Last active May 1, 2021 05:24
Volume rendering in Python using VTK-SimpleITK #python #visualization #vtk #simpleitk #itk
#!/usr/bin/python
import SimpleITK as sitk
import vtk
import numpy as np
from vtk.util.vtkConstants import *
def numpy2VTK(img,spacing=[1.0,1.0,1.0]):
# evolved from code from Stou S.,
@somada141
somada141 / build_opencv_anaconda.txt
Last active August 29, 2015 14:07
How to build OpenCV against Anaconda Python on Mac OSX #osx #build #cmake #opencv
sources:
http://smh84.wordpress.com/2014/04/23/install-opencv-in-anaconda-1-9-2-on-mac-os-x-mavericks/
https://gist.github.com/welch/6468594
It is a rite of passage to post one's successful build instructions for OpenCV on a Mac
after you've tried all the other conflicting instructions out there and still failed.
brew failed for me (was this because I could never get a happy brew doctor situation?
I'll never know). macports? nope. build-from-source recipes? I didn't find one that
worked for me.
@somada141
somada141 / tree_directory_ascii.md
Last active August 29, 2015 14:08
ASCII representation of the current directory structure #shell #os #unix #osx #bash

Firstly, define the tree command as such:

alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"

The cd to the directory in question and run tree. You should get an output such as this:

@somada141
somada141 / vtkimagedata_view_orthogonal.py
Last active August 29, 2015 14:08
Create an orthogonal slice view through a 3D vtkImageData object #python #vtk #imagedata #dicom #visualization
# Assuming that we have an object of type 'vtkImageData' under the name of
# 'image'
# Create a new vtkImageSliceMapper
mapper = vtk.vtkImageSliceMapper()
# Set 'image' as the input dataset
mapper.SetInput(image)
# Set the orthogonal slice orientation to be perpendicular to the 'X' axis. Use
# 'SetOrientationToY' and 'SetOrientationToZ' for the Y and Z axes.
# Alternatively you can use the 'SetOrientation' method with a 'int' parameter
@somada141
somada141 / vtk_resample_imagedata.md
Last active August 29, 2015 14:08
Resample a vtkImageData object using the vtkImageReslice class #python #vtk #imagedata #dicom #visualization

Assuming we have a vtkImageData object under the name of image this is how we'd go about resampling that image data with a factor of 2. Here we assume that image has a spacing of 1.0 mm across all axes

resliceFilter = vtk.vtkImageReslice()
resliceFilter.SetInput(image)
resliceFilter.SetOutputSpacing(0.5, 0.5, 0.5)
resliceFilter.SetInterpolationModeToCubic()
resliceFilter.Update()
imageResampled = resliceFilter.GetOutput()
@somada141
somada141 / vtk_load_stl.md
Last active January 27, 2022 06:46
Load an the contents of an STL file into a vtkPolyData object with Python and VTK #python #vtk #fileIO #visualizaton #stl

Assuming we have an .stl file under filenameSTL we can load the contents of that file using the vtkSTLReader class as such:

readerSTL = vtk.vtkSTLReader()
readerSTL.SetFileName(filenameSTL)
# 'update' the reader i.e. read the .stl file
readerSTL.Update()

polydata = readerSTL.GetOutput()
@somada141
somada141 / file_download_progressbar.py
Last active August 20, 2024 19:35
File download & progressbar with Python's urllib2 #python #fileIO #os #web #requests #urllib
# source:
# http://stackoverflow.com/questions/5783517/downloading-progress-bar-urllib2-python
import urllib2, sys
def chunk_report(bytes_so_far, chunk_size, total_size):
percent = float(bytes_so_far) / total_size
percent = round(percent*100, 2)
sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" %
(bytes_so_far, total_size, percent))
@somada141
somada141 / python_math_rotate_point.py
Last active May 27, 2022 02:10
Rotate a point coordinates around another point in Python #python #graphics #math
#source: http://stackoverflow.com/questions/20023209/python-function-for-rotating-2d-objects
import math
def rotatePolygon(polygon,theta):
"""Rotates the given polygon which consists of corners represented as (x,y),
around the ORIGIN, clock-wise, theta degrees"""
theta = math.radians(theta)
rotatedPolygon = []
for corner in polygon :