paparazzo
Silver Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору #include <windows.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <stdio.h> /* Written by Kenneth E. Moore July 1, 2003 February 3, 2004: added maximum angle feature. July 10, 2007: added support for importance sampling by adding TIS code. March 12, 2008: modified to include new docs and sample code on string passing capability. KEM May 1, 2008: modified to correct factor of 1/pi in the importance sampling logic (this had no affect on accuracy but was done to be consistent with conventions) KEM September 4, 2008: added docs for passing of the incident and substrate index, no affect on Lambertian scattering KEM */ int __declspec(dllexport) APIENTRY UserScatterDefinition(double *data); int __declspec(dllexport) APIENTRY UserParamNames(char *data); void CrossProduct(double x1, double y1, double z1, double x2, double y2, double z2, double *x3, double *y3, double *z3); void Normalize(double *x, double *y, double *z); BOOL WINAPI DllMain (HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) { return TRUE; } /* the data is stored as follows: data[ 0] = the total number of (double) values in the passed data array data[ 1] = x position of specular ray data[ 2] = y position of specular ray data[ 3] = z position of specular ray data[ 4] = x cosine of specular ray, on output it is the scattered ray data[ 5] = y cosine of specular ray, on output it is the scattered ray data[ 6] = z cosine of specular ray, on output it is the scattered ray data[ 7] = x normal data[ 8] = y normal data[ 9] = z normal data[10] = 0 initially if the DLL scatters the ray return 1 in data[10]. if the DLL returns full polarization data return 2 in data[10]. data[11] = millimeters per unit length (1.0 for mm, 25.4 for inches, 10.0 for cm and 1000.0 for meters) data[12] = relative energy (to be computed by the dll and returned) data[13] = incident media index data[14] = substrate media index data[15] = 1 for refraction, 0 for reflection data[16] = a random value to use as a seed data[17] = wavelength in µm // the following feature is used only for importance sampling data[18] = 0 normally if data[18] = -1, ZEMAX is requesting the DLL compute the total integrated scatter instead of the scattered ray vector. Once the TIS is computed, return the TIS in data[18]. If the DLL cannot compute the TIS (or the BSDF), ignore data[18] and leave the value unchanged. ZEMAX will then call the usual scatter algorithm and not use importance sampling. if data[18] = -2, ZEMAX is requesting the DLL compute the BSDF*cos(scatter_angle) instead of the scattered ray vector (this value multiplied by the solid angle should yield the total flux through that solid angle). Once the product BSDF*cos(scatter_angle) is computed, return the value in data[18]. If the DLL cannot compute the BSDF, ignore data[18] and leave the value unchanged. ZEMAX will then call the usual scatter algorithm and not use importance sampling. The BSDF in general depends upon the specular ray and the scattered ray ZEMAX has chosen to trace. The scattered ray ZEMAX has already chosen is stored in data[30] - data[32] below data[19] = 1 if ray has already bulk scattered data[20] = incident Ex real data[21] = incident Ex imaginary data[22] = incident Ey real data[23] = incident Ey imaginary data[24] = incident Ez real data[25] = incident Ez imaginary the following feature is used only for importance sampling, see discussion above data[30] = scattered ray x cosine data[31] = scattered ray y cosine data[32] = scattered ray z cosine data 40-45 need to be computed if the DLL sets data[10] = 2 data[40] = output Ex real data[41] = output Ex imaginary data[42] = output Ey real data[43] = output Ey imaginary data[44] = output Ez real data[45] = output Ez imaginary data[50] = The maximum number of parameters passed data[51] = input parameter 1 from user data[52] = input parameter 2 from user etc... up to data[50 + maxdata] where maxdata = int(data[50]) data[200] - data[249] = reserved block of data (400 bytes) for the data string argument data[250] - data[299] = reserved block of data (400 bytes) for the suggested path for the DLL data Return 0 if it works; else return -1. */ /* this DLL models an ideal Lambertian scattering surface */ /* if the max angle is greater than 0 and less than 90, the output cone angle is limited */ int __declspec(dllexport) APIENTRY UserScatterDefinition(double *data) { double px, py, pz, qx, qy, qz, nx, ny, nz,sx,sy,sz; double A, B, C, MAG, random_number, max_angle, R, T; char data_string1[400]; char data_string2[400]; // the following code is only used to implement importance sampling and is not required if (data[18] < 0) { if (data[18] == -1) { // compute the TIS and return the value in data[18] // the TIS may depend upon the specular ray. // For this simple Lambertian sample, TIS = 1 //data[18] = 3.14159265; data[18] = 1.0; return 0; } if (data[18] == -2) { // compute the BSDF*cos(scatter_angle) for the ray ZEMAX has chosen and return // the value in data[18] // For this simple Lambertian sample, BSDF = 1/pi // the scatter_angle is given by the dot product of the scattered ray and the // surface normal vector data[18] = fabs(data[30]*data[7] + data[31]*data[8] + data[32]*data[9]); data[18] /= 3.14159265; return 0; } if (data[18] == -3) { // the DLL is being called for the first time return 0; } if (data[18] == -4) { // the DLL is being called for the last time return 0; } return 0; } // this sample code does nothing, but illustrates how to read the string that may have been passed. // this string could be used to pass a data file name, for example memcpy(data_string1, &data[200], 400); memcpy(data_string2, &data[250], 400); /* we need some way of randomizing the random number generator */ srand((unsigned int) data[16]); /* return transmission */ T = data[51]; //data[12]= (data[17]+(0.0296875*(data[1]+3.2) + 0.51))/2; /*if (data[17]-(0.0296875*(data[1]+3.2) + 0.51)<0.02) { data[12]=1; } else { data[12]=0; }*/ data[12]=exp((-20000)*(0.0296875*(data[1]+3.2)-data[17] + 0.51)*( 0.0296875*(data[1]+3.2)-data[17] + 0.51)); /* return flag to indicate we scattered */ data[10] = 1.0; /* The main thing is to make sure the scattered ray lies within a hemisphere centered on the normal vector. The angle between the normal and the scattered ray must be less than max_angle degrees. */ sx = data[4]; // specular ray sy = data[5]; sz = data[6]; nx = data[7]; // normal vector ny = data[8]; nz = data[9]; /* Note the Lambertian distribution does not depend upon s. */ /* find vectors p,q perpindicular to n */ /* first pick any vector other than n */ if (fabs(nz) < 0.9) { px = 0.0; py = 0.0; pz = 1.0; } else { px = 1.0; py = 0.0; pz = 0.0; } /* this creates q normal to n */ CrossProduct(nx, ny, nz, px, py, pz, &qx, &qy, &qz); Normalize(&qx, &qy, &qz); /* this creates p normal to both q and n */ CrossProduct(nx, ny, nz, qx, qy, qz, &px, &py, &pz); //Normalize(&px, &py, &pz); // not needed since n and q are orthonormal already /* randomly choose new direction cosines */ data[4] = sx; data[5] = sy; data[6] = sz; /* that's the scattered ray! */ return 0; } int __declspec(dllexport) APIENTRY UserParamNames(char *data) { /* this function returns the name of the parameter requested */ int i; i = (int) data[0]; strcpy(data,""); if (i == 1) strcpy(data,"Transmission"); if (i == 2) strcpy(data,"Max Angle"); return 0; } void CrossProduct(double x1, double y1, double z1, double x2, double y2, double z2, double *x3, double *y3, double *z3) { *x3 = y1*z2 - z1*y2; *y3 = z1*x2 - x1*z2; *z3 = x1*y2 - y1*x2; } void Normalize(double *x, double *y, double *z) { double temp; temp = (*x)*(*x) +(*y)*(*y)+(*z)*(*z); temp = sqrt(temp); if (temp == 0) return; temp = 1.0/temp; *x *= temp; *y *= temp; *z *= temp; } | Всего записей: 3775 | Зарегистр. 06-04-2003 | Отправлено: 17:22 02-05-2014 | Исправлено: paparazzo, 17:24 02-05-2014 |
|