Thursday, March 19, 2009

Building LINQ Queries at Runtime in C#

Plugin Concept in .Net

Here is th VB.NET translation of this excellent Article

Imports System.Collections.Generic
Imports System.IO
Imports System.Reflection

Public Class PluginHandler

    Private Shared _Handler As PluginHandler
    Private _haPlugins = New System.Collections.Hashtable

    Public Shared Function Create() As PluginHandler
        If _Handler Is Nothing Then
            _Handler = New PluginHandler()
        End If
        Return _Handler
    End Function

    Public Function LoadPlugins() As Int16
        Dim PluginsLoaded = 0
        'Dim Path = "c:\OceanPlugins"
        Dim Path = System.Configuration.ConfigurationManager.AppSettings("RepertoirePlugins")
        Dim dir As DirectoryInfo = New DirectoryInfo(Path)
        Dim dllFiles() As FileInfo = dir.GetFiles("*.dll")
        For Each objInfo As FileInfo In dllFiles
            If LoadPlugin(objInfo.FullName) Then
                PluginsLoaded += 1
            End If
        Return PluginsLoaded
    End Function

    Public Function LoadPlugin(ByVal sFile As String) As Boolean
        Dim assPlugin As Assembly = Assembly.LoadFile(sFile)
        Dim typPluginTypes() As Type = assPlugin.GetTypes()
        Dim bReturn As Boolean = False

        For Each typPlugin In typPluginTypes
            Dim typInterface As System.Type = typPlugin.GetInterface("Ocean.IOceanPlugin")
            If typInterface IsNot Nothing And typPlugin.IsClass Then
                Dim objFactory As Object = assPlugin.CreateInstance(typPlugin.FullName)
                Dim objPlugin As Ocean.IOceanPlugin = CType(objFactory, Ocean.IOceanPlugin)
                _haPlugins.Add(objPlugin.GetShortDescription, objPlugin)                
                bReturn = True
            End If
    End Function

    Public Function getPlugins() As Hashtable
        Return _haPlugins
    End Function

End Class

Tuesday, March 17, 2009

Raytracing from SIGGRAPH

Raytracing Courses

Raytracing : Compute Intersection

Python Raytracer in 50 Lines of Code


import math
from PIL import Image

def sub(v1, v2): return [x-y for x,y in zip(v1, v2)]
def dot(v1, v2): return sum([x*y for x,y in zip(v1, v2)])
def norm(v): return [x/math.sqrt(dot(v,v)) for x in v]

def trace_sphere(r, s, tmin, color):
sr = sub(s[0], r[0])
dsr = dot(sr, r[1])

d = dsr*dsr - dot(sr,sr) + s[1]
if d < 0: return
d = math.sqrt(d)

t1 = dsr + d
if t1 < 0: return

t2 = dsr - d
if t2 > tmin[0]: return

if t2 < 0:
tmin[0] = t1
tmin[0] = t2
color[:] = [s[2]]

def trace_ray(ray, spheres):
color = [(0, 0, 0)]
tmin = [100000]
for s in spheres:
trace_sphere(ray, s, tmin, color)
return color[0]

red = (255,0,0)
blue = (0,0,255)
spheres = ( ((0,0,100), 100.0, red), ((0,-5,50), 20.0, blue) )

w, h = 200, 200

image ="RGB", (w, h))
pix = image.load()

for x in range(w):
for y in range(h):
ray = ( (0,0,0), norm(((x-w/2.0)/w, (y-h/2.0)/w, 1)) )
pix[x,y] = trace_ray(ray, spheres)"out.png")

Simple Ray Tracing Computation

Example of a simple Ray Tracing Computation to compute primary ray and test intersection with a sphere

View Database Structure Using C#

ASP.NET IronPython Resource