a mel delete attributes in maya after import fbx form 3dmax - maya

20180509
Thanks for your information.
I'd finished mel like that.
select -all -hi;
string $allObj[] =`ls -sl -fl `;
for($i=0;$i<`size$allObj`;$i++)
{
select -r $allObj[$i];
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032useFBXASC032globalFBXASC032settings"`;
if ($attrExists) {
deleteAttr -at "mrFBXASC032displacementFBXASC032useFBXASC032globalFBXASC032settings" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032viewFBXASC032dependent"`;
if ($attrExists) {
deleteAttr -at "mrFBXASC032displacementFBXASC032viewFBXASC032dependent" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032method"`;
if ($attrExists) {
deleteAttr -at "mrFBXASC032displacementFBXASC032method" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032smoothingFBXASC032on"`;
if ($attrExists) {
deleteAttr -at "mrFBXASC032displacementFBXASC032smoothingFBXASC032on" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032edgeFBXASC032length"`;
if ($attrExists) {
deleteAttr -at "mrFBXASC032displacementFBXASC032edgeFBXASC032length" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032maxFBXASC032displace"`;
if ($attrExists) {
deleteAttr -at "mrFBXASC032displacementFBXASC032maxFBXASC032displace" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032parametricFBXASC032subdivisionFBXASC032level"`;
if ($attrExists) {
deleteAttr -at "mrFBXASC032displacementFBXASC032parametricFBXASC032subdivisionFBXASC032level" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "UDP3DSMAX"`;
if ($attrExists) {
deleteAttr -at "UDP3DSMAX" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "MaxHandle"`;
if ($attrExists) {
deleteAttr -at "MaxHandle" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "MaxVisibility"`;
if ($attrExists) {
deleteAttr -at "MaxVisibility" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "currentUVSet"`;
if ($attrExists) {
deleteAttr -at "currentUVSet" $allObj[$i];
}
}
Show another error
Cannot delete static attribute 'currentUVSet' from node 'ObjectName'
Does children of a compound attribute cannot be deleted?
Or is it not nesserary to delete attribute 'currentUVSet'?
20180508
After I import fbx file to maya from 3dmax.
Those objects have some attributes that I don't need.
"mrFBXASC032displacementFBXASC032useFBXASC032globalFBXASC032settings"
"mrFBXASC032displacementFBXASC032viewFBXASC032dependent"
"mrFBXASC032displacementFBXASC032method"
"mrFBXASC032displacementFBXASC032smoothingFBXASC032on"
"mrFBXASC032displacementFBXASC032edgeFBXASC032length"
"mrFBXASC032displacementFBXASC032maxFBXASC032displace"
"mrFBXASC032displacementFBXASC032parametricFBXASC032subdivisionFBXASC032level"
"UDP3DSMAX"
"MaxHandle"
"currentUVSet"
I try to have a mel delete them.
string $allObj[] =`ls -sl -fl `;
for($i=0;$i<`size$allObj`;$i++)
{
select -r $allObj[$i];
deleteAttr -at "mrFBXASC032displacementFBXASC032useFBXASC032globalFBXASC032settings";
deleteAttr -at "mrFBXASC032displacementFBXASC032viewFBXASC032dependent";
deleteAttr -at "mrFBXASC032displacementFBXASC032method";
deleteAttr -at "mrFBXASC032displacementFBXASC032smoothingFBXASC032on";
deleteAttr -at "mrFBXASC032displacementFBXASC032edgeFBXASC032length";
deleteAttr -at "mrFBXASC032displacementFBXASC032maxFBXASC032displace";
deleteAttr -at "mrFBXASC032displacementFBXASC032parametricFBXASC032subdivisionFBXASC032level";
deleteAttr -at "UDP3DSMAX";
deleteAttr -at "MaxHandle";
deleteAttr -at "currentUVSet";
}
}
If I select all objects. mel will show error message like :
does not have attribute 'UDP3DSMAX'
Because those objects don't have the same nodes.
How to delete all attributes at correct way?

You can reduce the complexity a littel bit if you use the catch command:
string $attr[] = {"mrFBXASC032displacementFBXASC032useFBXASC032globalFBXASC032settings"
"mrFBXASC032displacementFBXASC032viewFBXASC032dependent"
"mrFBXASC032displacementFBXASC032method"
"mrFBXASC032displacementFBXASC032smoothingFBXASC032on"
"mrFBXASC032displacementFBXASC032edgeFBXASC032length"
"mrFBXASC032displacementFBXASC032maxFBXASC032displace"
"mrFBXASC032displacementFBXASC032parametricFBXASC032subdivisionFBXASC032level"
"UDP3DSMAX"
"MaxHandle"
"currentUVSet"};
string $sel[] = `ls -sl`;
for($i = 0; $i < size($sel); $i++)
{
for( $k = 0; $k < size($attr); $k++)
{
catch(`deleteAttr -at $attr[$k] $sel[$i]`);
}
}
This way you do not have to care about non existing attributes, but of course you do not see other errors that may occurs.

Before calling deleteAttr, you can check if the attribute exists first:
int $attrExists = `attributeQuery -node yourObject -exists "yourAttr"`;
if ($attrExists) {
deleteAttr -at "yourAttr" youObject;
}

Thansks for help
The last mel like that
global proc SB_CheckFile()
{
select -all -hi;
string $allObj[] =`ls -sl -fl `;
for($i=0;$i<`size$allObj`;$i++)
{
select -r $allObj[$i];
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032useFBXASC032globalFBXASC032settings"`;
if ($attrExists)
{
deleteAttr -at "mrFBXASC032displacementFBXASC032useFBXASC032globalFBXASC032settings" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032viewFBXASC032dependent"`;
if ($attrExists)
{
deleteAttr -at "mrFBXASC032displacementFBXASC032viewFBXASC032dependent" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032method"`;
if ($attrExists)
{
deleteAttr -at "mrFBXASC032displacementFBXASC032method" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032smoothingFBXASC032on"`;
if ($attrExists)
{
deleteAttr -at "mrFBXASC032displacementFBXASC032smoothingFBXASC032on" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032edgeFBXASC032length"`;
if ($attrExists)
{
deleteAttr -at "mrFBXASC032displacementFBXASC032edgeFBXASC032length" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032maxFBXASC032displace"`;
if ($attrExists)
{
deleteAttr -at "mrFBXASC032displacementFBXASC032maxFBXASC032displace" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "mrFBXASC032displacementFBXASC032parametricFBXASC032subdivisionFBXASC032level"`;
if ($attrExists)
{
deleteAttr -at "mrFBXASC032displacementFBXASC032parametricFBXASC032subdivisionFBXASC032level" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "UDP3DSMAX"`;
if ($attrExists)
{
deleteAttr -at "UDP3DSMAX" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "MaxHandle"`;
if ($attrExists)
{
deleteAttr -at "MaxHandle" $allObj[$i];
}
int $attrExists = `attributeQuery -node $allObj[$i] -exists "MaxVisibility"`;
if ($attrExists)
{
deleteAttr -at "MaxVisibility" $allObj[$i];
}
}
select (`ls -type "transform"`);
string $allObj[] =`ls -sl -fl `;
for($i=0;$i<`size$allObj`;$i++)
{
select -r $allObj[$i];
int $attrExists = `attributeQuery -node $allObj[$i] -exists "currentUVSet"`;
if ($attrExists)
{
deleteAttr -at "currentUVSet" $allObj[$i];
}
}
}

Related

Difficulty writing a loop basing on user choice

I'm having difficulty writing a loop that will ask a user if he/she wants to continue the purchase of the products. It's part of my coursework at school. I have written all the required functions but Im struggling to get the loop to work.
Basically, what I want the loop to do is ask a user if he/she wants to continue the purchase and the answer should be either Y or N. Which will then take the remaining budget, implement it back to the program and run it again with that budget in mind, then deduct the value of the product chosen.
I have been trying to write that loop for more than 2 hours now and I'm running out of ideas.
That's the code I'm working with:
#include <stdio.h>
int getItemPrice(char itemPrefix, int applePrice, int orangePrice, int pearPrice);
displayMenu(int applePrice, int orangePrice, int pearPrice);
withinBudget(int budget, char purchase, int applePrice, int orangePrice, int pearPrice);
purchaseItem(int budget, char purchase, int applePrice, int orangePrice, int pearPrice);
int main()
{
int orangePrice, applePrice, pearPrice, budget;
char purchase;
printf("*****************\n");
printf("Item prefixes\n");
printf("A: Apple\n");
printf("O: Orange\n");
printf("P: Pear\n");
printf("*****************\n\n");
printf("***************\n");
printf("*** MyStore ***\n");
printf("***************\n\n");
printf("**** SHOPKEEPER PANEL ****\n");
printf("Welcome to the store. Please enter the prices for the following products: \n");
printf("Please enter the price for the Orange: \x9C");
scanf_s("%d", &orangePrice);
printf("Please enter the price for the Apple: \x9C");
scanf_s("%d", &applePrice);
printf("Please enter the price for the Pear: \x9C");
scanf_s("%d", &pearPrice);
printf("\n\n");
displayMenu(applePrice, orangePrice, pearPrice);
printf("**** Customer menu ****\n");
printf("Please enter your budget: \x9C");
scanf_s("%d", &budget);
printf("Please enter the item you would like to purchase using the item Prefix: ");
scanf_s(" %c", &purchase, 1);
printf("\n\n");
displayMenu(applePrice, orangePrice, pearPrice);
withinBudget(budget, purchase, applePrice, orangePrice, pearPrice);
purchaseItem(budget, purchase, applePrice, orangePrice, pearPrice);
return 0;
};
int getItemPrice(char itemPrefix, int applePrice, int orangePrice, int pearPrice)
{
if (itemPrefix == 'A') {
return applePrice;
}
else if (itemPrefix == 'O') {
return orangePrice;
}
else if (itemPrefix == 'P') {
return pearPrice;
}
else {
return -1;
}
return getItemPrice;
}
displayMenu(int applePrice, int orangePrice, int pearPrice)
{
printf("**** Shop Menu ****\n");
printf("Item:\t\tPrice\n");
printf("A:\t\t\x9C%d\n", applePrice);
printf("O:\t\t\x9C%d\n", orangePrice);
printf("P:\t\t\x9C%d\n", pearPrice);
printf("\n\n");
return 0;
}
withinBudget(int budget, char purchase, int applePrice, int orangePrice, int pearPrice)
{
int getItemPrice(itemPrefix, applePrice, orangePrice, pearPrice);
if (purchase == 'A') {
return applePrice;
}
else if (purchase == 'O') {
return orangePrice;
}
else if (purchase == 'P') {
return pearPrice;
}
else {
return -1;
}
}
purchaseItem(int budget, char purchase, int applePrice, int orangePrice, int pearPrice)
{
int items ;
int calculation;
if (purchase == 'A') {
items = applePrice;
}
else if (purchase == 'O') {
items = orangePrice;
}
else if (purchase == 'P') {
items = pearPrice;
}
else {
items = -1;
}
if (items != -1) {
calculation = budget - items;
if (calculation >= 0) {
printf("Purchase was a success!\n");
printf("Purchase details\n");
printf("----------------------\n");
printf("Item: %c\n", purchase);
printf("Price: \x9C%d\n", items);
printf("Remaining budget: \x9C%d\n\n", calculation);
printf("Thanks for shopping with us!");
}
else {
items = -1;
}
}
if (items == -1) {
printf("Purchase FAILED!\n");
printf("Low budget or invalid item!\n\n");
printf("Thanks for shopping with us!");
}
;
I have already tried writing the while loop in the body of the program as well as inside of the purchase item function. I'm completely clueless at this point and don't really know what can I do to make it work. Or what I'm not doing that would help me this program to work as I want it to.
Any help would be appreciated. As well as any tips on what can I do to improve it.
You can implement a loop logic like this:
int main(void){
... // Your definitons
bool proceed = true;
while(proceed){
... // All your code here
bool isValid = false; // Check the answer character's validity
while(!isValid){
printf("Would you like to purchase anything else? [Y / N]");
char answer = getchar();
putchar('\n'); // Add a line feed
if (answer == 'Y' || answer == 'y'){
proceed = true;
isValid = true;
}
else if (answer == 'N' || answer == 'n'){
proceed = false;
isValid = true;
}
else{
// Entered an unexpected character, force the user to enter a valid character
printf("Please enter a valid answer\n");
isValid = false;
}
}
}
return 0;
}

Rubberband effect for scrolling containers

I'd like to have a rubberband effect on scrolling containers for which I feel the "tensile scrolling" that is build into the Component base class is no sufficient replacement.
Is there a reasonably feasible way like disabling the default overscroll behavior in order to control the property scrollY in a way like in this example - How to create the rubberband effect?
Since there was no answer to my question I answer it myself. Beware that I am not an expert and therefore this might not fit Your purpose!
The default overscroll behaviour can be overridden by extending the Container class and overriding the pointer methods without calling their super methods.
Here is an example:
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Form;
import com.codename1.ui.Graphics;
import com.codename1.ui.Label;
import com.codename1.ui.animations.Motion;
import com.codename1.ui.geom.Point;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
public class FormOverscroll extends Form {
FormOverscroll() {
super("FormOverscroll");
setScrollable(false);
setLayout(new BorderLayout());
Container container = new Container(BoxLayout.y()) {
boolean bounce = false;
Motion motion = null;
Point pointPressed = null;
long millisPoint = 0;
int scrollYPressed = 0, scrollYPrevious = 0;
{
// setTensileDragEnabled(false);
setScrollableY(true);
}
#Override
protected boolean isStickyDrag() {
return true;
}
#Override
public void pointerPressed(int x, int y) {
pointPressed = new Point(x, y);
scrollYPressed = scrollYPrevious = getScrollY();
millisPoint = System.currentTimeMillis();
motion = null;
bounce = false;
}
#Override
public void pointerDragged(int x, int y) {
if (null == pointPressed) {
return;
}
int yDist = y - pointPressed.getY();
int scrollY = scrollYPressed - yDist;
if (scrollY < 0) {
Motion motionRubberband = Motion.createCubicBezierMotion(0, scrollY, getHeight(), 0f, 1.2f, 0.5f, 0.6f);
motionRubberband.setStartTime(0);
motionRubberband.setCurrentMotionTime(Math.abs(scrollY));
scrollY = motionRubberband.getValue();
}
setScrollY(scrollY);
}
float getVelocity(int scrollY) {
long millisNow = System.currentTimeMillis();
long timediff = millisNow - millisPoint;
float diff = scrollYPrevious - scrollY;
float velocity = (diff / timediff) * -1f;
scrollYPrevious = scrollY;
millisPoint = millisNow;
return velocity;
}
#Override
public void pointerReleased(int x, int y) {
if (null == pointPressed) {
return;
}
int yDist = y - pointPressed.getY();
int scrollY = scrollYPressed - yDist;
float velocity = getVelocity(scrollY);
motion = Motion.createFrictionMotion(scrollY, Integer.MIN_VALUE, velocity, 0.0007f);
motion.start();
getComponentForm().registerAnimated(this);
pointPressed = null;
}
#Override
public boolean animate() {
boolean animate = super.animate();
if (null != motion) {
int scrollY = motion.getValue();
setScrollY(scrollY);
int target = 0;
if (scrollY < target && !bounce) {
createRubberbandMotion(scrollY, target);
bounce = true;
motion.start();
}
int maxScrollY = Math.max(target, getScrollDimension().getHeight() - getHeight());
if (scrollY > maxScrollY && !bounce) {
createRubberbandMotion(scrollY, maxScrollY);
bounce = true;
motion.start();
}
scrollYPrevious = scrollY;
if (motion.isFinished()) {
motion = null;
}
return true;
}
return animate;
}
private void createRubberbandMotion(int source, int target) {
motion = Motion.createCubicBezierMotion(source, target, 500, 0.0f, 1.0f, 1.2f, 1.0f);
}
#Override
public Component getComponentAt(int x, int y) {
return this;
}
#Override
public void paint(Graphics aGraphics) {
super.paint(aGraphics);
}
};
for (int index = 0; index < 100; index++) {
container.add(new Label("Zeile " + index));
}
add(BorderLayout.CENTER, container);
}
}

OpenGL Exception thrown: read access violation, window was 0xCCCCCCCC

I am using the same framework which I had described in the previous question.
I solved it by creating a fresh dll instead of just changing the build type of the project from 'Windows Application (.exe)' to 'DLL (.dll)'.
But now when I use a variable of type GLFWwindow* in my structure and try to write or read. It always causes to pop up write access or read access violation respectively. The exception comes abruptly just as the window starts and then closes, showing the exception.
The exception says the following:-
Exception thrown: read access violation.
window was 0xCCCCCCCC.
It happens in the window.c file of GLFW and it points to the function which tries to read it. I even tried to use pointers to modify the window but still it didn't work.
Here is the code for the framework:- Modified!
window.h
#ifndef LIB_GRAPHICS
#define LIB_GRAPHICS
#ifdef LIB_EXPORTS
#define LIB_EXPORT _declspec(dllexport)
#else
#define LIB_EXPORT _declspec(dllimport)
#endif
#define LIB_FALSE 0
#define LIB_TRUE 1
#define LIB_BeginRender LIB_SetEvents(); LIB_ClearToColor
#define LIB_EndRender LIB_SwapWindowBuffers
#define LIB_CENTER_POSITION 0xCEAAFFEE
/* Define other things... */
typedef const char* LIB_String;
typedef unsigned LIB_Integer;
typedef char LIB_Char;
typedef int LIB_Bool;
/* Define the structures... */
typedef struct LIB_Window LIB_Window;
typedef struct LIB_Color
{
int r;
int g;
int b;
int a;
} LIB_Color;
/* Constructors, destructors and other functions... */
LIB_Bool LIB_EXPORT LIB_Initialize();
void LIB_EXPORT LIB_SetEvents();
void LIB_EXPORT LIB_ClearToColor(const LIB_Color color);
void LIB_EXPORT LIB_SetFrameColor(const LIB_Color color);
LIB_EXPORT LIB_Window* LIB_CreateWindow(const LIB_String title, const int x, const int y, const int width, const int height, const LIB_Bool resizable, const LIB_Bool fullscreen);
void LIB_EXPORT LIB_GetDisplaySize(int *width, int *height);
void LIB_EXPORT LIB_GetWindowFrameSize(LIB_Window* window, int *width, int *height);
void LIB_EXPORT LIB_GetWindowCursorPosition(LIB_Window* window, float *x, float *y);
void LIB_EXPORT LIB_GetWindowPosition(LIB_Window* window, int *x, int *y);
void LIB_EXPORT LIB_GetWindowSize(LIB_Window* window, int *width, int *height);
LIB_String LIB_EXPORT LIB_GetWindowTitle(LIB_Window* window);
LIB_Bool LIB_EXPORT LIB_IsWindowFullScreen(LIB_Window* window);
LIB_Bool LIB_EXPORT LIB_IsWindowOpened(LIB_Window* window);
void LIB_EXPORT LIB_SwapWindowBuffers(LIB_Window* window);
void LIB_EXPORT LIB_SetWindowPosition(LIB_Window* window, const int x, const int y);
void LIB_EXPORT LIB_SetWindowSize(LIB_Window* window, const int width, const int height);
void LIB_EXPORT LIB_SetWindowTitle(LIB_Window * window, const LIB_String title);
void LIB_EXPORT LIB_SetFullScreenState(LIB_Window * window, const LIB_Bool fullscreen);
void LIB_EXPORT LIB_DestroyWindow(LIB_Window* window);
void LIB_EXPORT LIB_Terminate();
#endif /* LIB_GRAPHICS */
window.c
#include "window.h"
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
/* Create the structures... */
struct LIB_Window
{
LIB_String title;
int x;
int y;
int width;
int height;
LIB_Bool fullscreen;
GLFWwindow** window;
};
/* Start the functions here... */
LIB_Bool LIB_Initialize()
{
return (LIB_Bool)glfwInit();
}
void LIB_SetEvents()
{
glfwPollEvents();
}
void LIB_ClearToColor(const LIB_Color color)
{
glClearColor((float)color.r / 255, (float)color.g / 255, (float)color.b / 255, (float)color.a / 255);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}
void LIB_SetFrameColor(const LIB_Color color)
{
glClearColor((float)color.r / 255, (float)color.g / 255, (float)color.b / 255, (float)color.a / 255);
}
LIB_Window* LIB_CreateWindow(const LIB_String title, int x, int y, const int width, const int height, const LIB_Bool resizable, const LIB_Bool fullscreen)
{
LIB_Window wind;
wind.title = title;
if (x == LIB_CENTER_POSITION)
{
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
x = (mode->width - width) / 2;
}
wind.x = x;
if (y == LIB_CENTER_POSITION)
{
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
y = (mode->height - height) / 2;
}
wind.y = y;
wind.width = width;
wind.height = height;
wind.fullscreen = fullscreen;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_RESIZABLE, resizable);
wind.window = NULL;
if (fullscreen == 1)
{
wind.window = (GLFWwindow**)glfwCreateWindow(width, height, title, glfwGetPrimaryMonitor(), NULL);
}
else if (fullscreen == 0)
{
wind.window = (GLFWwindow**)glfwCreateWindow(width, height, title, NULL, NULL);
}
glfwSetWindowPos((GLFWwindow*)wind.window, x, y);
int screen_width, screen_height;
glfwGetFramebufferSize((GLFWwindow*)wind.window, &screen_width, &screen_height);
if (wind.window == NULL)
{
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent((GLFWwindow*)wind.window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
return NULL;
}
glViewport(0, 0, screen_width, screen_height);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return &wind;
}
void LIB_GetWindowFrameSize(LIB_Window * window, int *width, int *height)
{
int screenwidth, screenheight;
glfwGetFramebufferSize(((GLFWwindow*)window->window), &screenwidth, &screenheight);
*width = screenwidth;
*height = screenheight;
}
void LIB_GetWindowCursorPosition(LIB_Window * window, float *x, float *y)
{
double cx, cy;
glfwGetCursorPos(((GLFWwindow*)window->window), &cx, &cy);
*x = (float)cx;
*y = (float)cy;
}
void LIB_GetDisplaySize(int *width, int *height)
{
const struct GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
*width = mode->width;
*height = mode->height;
}
void LIB_GetWindowPosition(LIB_Window * window, int *x, int *y)
{
*x = (window)->x;
*y = (window)->y;
}
void LIB_GetWindowSize(LIB_Window * window, int *width, int *height)
{
*width = (window)->width;
*height = (window)->height;
}
LIB_String LIB_GetWindowTitle(LIB_Window * window)
{
return (window)->title;
}
LIB_Bool LIB_IsWindowFullScreen(LIB_Window * window)
{
return (window)->fullscreen;
}
LIB_Bool LIB_IsWindowOpened(LIB_Window * window)
{
return !glfwWindowShouldClose(((GLFWwindow*)window->window));
}
void LIB_SwapWindowBuffers(LIB_Window * window)
{
glfwSwapBuffers(((GLFWwindow*)window->window));
}
void LIB_SetWindowPosition(LIB_Window * window, const int x, const int y)
{
glfwSetWindowPos(((GLFWwindow*)window->window), x,y);
(window)->x = x;
(window)->y = y;
}
void LIB_SetWindowSize(LIB_Window * window, const int width, const int height)
{
glfwSetWindowSize(((GLFWwindow*)window->window), width, height);
(window)->width = width;
(window)->height = height;
}
void LIB_SetWindowTitle(LIB_Window * window, const LIB_String title)
{
glfwSetWindowTitle(((GLFWwindow*)window->window), title);
(window)->title = title;
}
void LIB_SetFullScreenState(LIB_Window * window, const LIB_Bool fullscreen)
{
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
if (fullscreen == LIB_FALSE)
{
glfwSetWindowMonitor(((GLFWwindow*)window->window), NULL, (window)->x, (window)->y, (window)->width, (window)->height, 60);
}
else if (fullscreen == LIB_TRUE)
{
glfwSetWindowMonitor(((GLFWwindow*)window->window), glfwGetPrimaryMonitor(), 0, 0, mode->width, mode->height, 60);
}
(window)->fullscreen = fullscreen;
}
void LIB_DestroyWindow(LIB_Window * window)
{
(window)->window = NULL;
(window)->title = NULL;
(window)->x = 0;
(window)->y = 0;
(window)->width = 0;
(window)->height = 0;
free(window);
}
void LIB_Terminate()
{
printf("BLITZ terminated by the user!!\n");
glfwTerminate();
}
The function LIB_CreateWindow returns a pointer to the loacal variable LIB_Window wind;. Once the function terminates, the variable goes out of scope and pointer points to "nowhere". This is an undefined behavior. Note, a local variable in a function is allocated on the stack, which is immediately freed, when the function is terminated.
You can fix this quickly, by declare the variable static:
static LIB_Window wind;
But, if you do so, then of course the library can only manage one window.
Either you create a dynamically allocated variable,
void LIB_CreateWindow( ..... )
{
.....
LIB_Window *newWnd = malloc( sizeof(LIB_Window) );
*newWnd = wind;
return newWnd;
}
or you declare the variable of type LIB_Window outside the function and provide it to the function by pointer:
void LIB_CreateWindow( LIB_Window *ptr_wnd, ..... );
int main( void )
{
.....
LIB_Window wind;
LIB_CreateWindow( &wnd, ..... );
.....
}
Of course, the memory release of the LIB_Window data structure, in the function LIB_DestroyWindow, only works if the memory for the variable was dynamically allocated:
LIB_DestroyWindow()
{
.....
free(window); // <--- this only works if window was allocated by malloc
}

Get minimum value in an array and then get index

I want to get the minimum value in an array and then get the index of that item, in one step without writing my own loop (if I have to please let me know).
I know I can just do the
$b = ($a | Measure -Minimum).Minimum
But then I have to do
[array]::IndexOf($a, $b)
And while that is normally okay, I'm looking for a way to do it once because I'm running this MANY MANY times in a loop.
Thanks!
EDIT: One step meaning without looping through the array twice
Personally, I might consider a different data structure. Maybe something sorted to begin with...
This code may work for your needs:
$myArray = 5,66,4,33,2,9,9,12
$index = 0
$minIndex = 0
$minValue = [int]::MaxValue
$myArray | % { if ($minValue -gt $_) {$minValue = $_; $minIndex = $index}; $index++ }
"MinIndex $minIndex = MinValue $minValue"
its a problem of type, try like this:
$myArray = [int[]]5,66,4,33,2,9,9,12
$minvalue=[int]($myArray | measure -Minimum).Minimum
$myArray.IndexOf($minvalue)
Here are 6 different options for you...
cls
$myArray = #(5,66,4,33,2,9,9,12)
$iterations = 50000
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minValue = [int]($myArray | Measure-Object -Minimum).Minimum
$minIndex = $myArray.IndexOf($minValue)
}
}).TotalSeconds
"measure-object with indexOf: $t"
$t = (measure-command{
foreach ($i in 1..$iterations) {
$index = 0
$minIndex = 0
$minValue = [int]::MaxValue
$myArray | % { if ($minValue -gt $_) {$minValue = $_; $minIndex = $index}; $index++ }
}
}).TotalSeconds
"pipeline with compare: $t"
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minIndex = 0
$minValue = [int]::MaxValue
foreach ($index in 0..($myArray.count-1)) {
if ($myArray[$index] -lt $minValue) {
$minValue = $myArray[$index]
$minIndex = $index
}
}
}
}).TotalSeconds
"foreach-loop with compare: $t"
$t = (measure-command{
foreach ($i in 1..$iterations) {
$h = [System.Collections.ArrayList]::new()
foreach ($index in 0..($myArray.count-1)) {
$null = $h.add([tuple]::Create($myArray[$index], $index))
}
$h.Sort()
$minIndex = $h[0].Item2
}
}).TotalSeconds
"quicksort of a list of tuples: $t"
Add-Type -TypeDefinition #"
using System;
using System.Linq;
public static class My {
public static int indexOfMin(int[] arr){
int min = arr.Min();
return Array.IndexOf(arr, min);
}
}
"#
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minIndex = [my]::indexOfMin($myArray)
}
}).TotalSeconds
"custom type and IndexOf: $t"
Add-Type #"
public static int[] indexOfMin(int[] arr){
int min = int.MaxValue;
int minIndex = 0;
for (int i = 0; i < arr.Length; i++) {
if (arr[i]<min) {
min = arr[i];
minIndex = i;
}
}
return new int[] {min, minIndex};
}
"# -name My2 -Namespace System
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minValue, $minIndex = [my2]::indexOfMin($myArray)
}
}).TotalSeconds
"custom type and looping: $t"

Test Case for Trigger(before insert, before update) in Apex

Anyone please help me to write test case for 100% code coverage for following trigger in apex
trigger ETicketAirlineUrlTrigger on E_Ticket__c (before insert, before update) {
for(E_Ticket__c etNew : Trigger.New)
{
if(etNew.Airline__c =='Virgin Australia')
{
etNew.Airlines_Url__c = 'Virgin Australia';
}
else if(etNew.Airline__c =='Virgin Atlantic')
{
etNew.Airlines_Url__c = 'Virgin Atlantic';
}
else if(etNew.Airline__c =='Virgin America')
{
etNew.Airlines_Url__c = 'Virgin America';
}
else if(etNew.Airline__c =='US AIR')
{
etNew.Airlines_Url__c = 'US AIR';
}
else if(etNew.Airline__c =='United Airlines')
{
etNew.Airlines_Url__c = 'United Airlines';
}
else if(etNew.Airline__c =='Turkish Air')
{
etNew.Airlines_Url__c = 'Turkish Air';
}
else if(etNew.Airline__c =='Thai Air')
{
etNew.Airlines_Url__c = 'Thai Air';
}
else if(etNew.Airline__c =='Swiss Airlines')
{
etNew.Airlines_Url__c = 'Swiss Airlines';
}
else if(etNew.Airline__c =='Southwest Airlines')
{
etNew.Airlines_Url__c = 'Southwest Airlines';
}
else if(etNew.Airline__c =='South African')
{
etNew.Airlines_Url__c = 'South African';
}
else if(etNew.Airline__c =='Singapore Airlines')
{
etNew.Airlines_Url__c = 'Singapore Airlines';
}
else if(etNew.Airline__c =='QATAR AIRWAYS')
{
etNew.Airlines_Url__c = 'QATAR AIRWAYS';
}
else if(etNew.Airline__c =='Qantas Airlines')
{
etNew.Airlines_Url__c = 'Qantas Airlines';
}
else if(etNew.Airline__c =='Malaysia')
{
etNew.Airlines_Url__c = 'Malaysia';
}
else if(etNew.Airline__c =='Lufthansa')
{
etNew.Airlines_Url__c = 'Lufthansa';
}
else if(etNew.Airline__c =='KLM - Royal Dutch Airlines')
{
etNew.Airlines_Url__c = 'KLM - Royal Dutch Airlines';
}
else if(etNew.Airline__c =='Japan Air')
{
etNew.Airlines_Url__c = 'Japan Air';
}
else if(etNew.Airline__c =='Etihad Airlines')
{
etNew.Airlines_Url__c = 'Etihad Airlines';
}
else if(etNew.Airline__c =='Emirates')
{
etNew.Airlines_Url__c = 'Emirates';
}
else if(etNew.Airline__c =='Delta Airlines')
{
etNew.Airlines_Url__c = 'Delta Airlines';
}
else if(etNew.Airline__c =='Copa Airlines')
{
etNew.Airlines_Url__c = 'Copa Airlines';
}
else if(etNew.Airline__c =='Continental Airlines')
{
etNew.Airlines_Url__c = 'Continental Airlines';
}
else if(etNew.Airline__c =='Cathay Pacific')
{
etNew.Airlines_Url__c = 'Cathay Pacific';
}
else if(etNew.Airline__c =='British Airways')
{
etNew.Airlines_Url__c = 'British Airways';
}
else if(etNew.Airline__c =='American Airlines')
{
etNew.Airlines_Url__c = 'American Airlines';
}
else if(etNew.Airline__c =='All Nippon')
{
etNew.Airlines_Url__c = 'All Nippon';
}
else if(etNew.Airline__c =='Alitalia')
{
etNew.Airlines_Url__c = 'Alitalia';
}
else if(etNew.Airline__c =='Alaska Airlines')
{
etNew.Airlines_Url__c = 'Alaska Airlines';
}
else if(etNew.Airline__c =='Air New Zealand')
{
etNew.Airlines_Url__c = 'Air New Zealand';
}
else if(etNew.Airline__c =='Air France')
{
etNew.Airlines_Url__c = 'Air France';
}
else if(etNew.Airline__c =='Air Canada')
{
etNew.Airlines_Url__c = 'Air Canada';
}
}
}
And not able to get 100% code coverage using following test case
#isTest
private class ETicketAirlineUrlTestCase {
static testMethod void myUnitTest() {
Opportunity oppNew = new Opportunity();
oppNew.Name = 'Test Opp';
oppNew.StageName = 'To Be Searched';
oppNew.CloseDate = System.now().date();
insert oppNew;
E_Ticket__c et = new E_Ticket__c();
et.Airline__c = 'Virgin Australia';
et.Opportunity__c = oppNew.Id;
et.Airline__c = 'Virgin Atlantic';
insert et;
E_Ticket__c etNew = [select Id, Airline__c from E_Ticket__c where Id = :et.Id];
update etNew;
}
}
when i run Class-->Force.com-->Run Test it show 100% code coverage
but when i do Force.com-->Deploy To Server it show error 6% covered
Any help would be appreciated
Thanks..
Try this:
#isTest
private class ETicketAirlineUrlTestCase {
static testMethod void myUnitTest() {
Opportunity oppNew = new Opportunity();
oppNew.Name = 'Test Opp';
oppNew.StageName = 'To Be Searched';
oppNew.CloseDate = System.now().date();
insert oppNew;
List<E_Ticket__c> tickets = new List<E_Ticket__c>();
E_Ticket__c et1 = new E_Ticket__c();
et1.Airline__c = 'Virgin Australia';
et1.Opportunity__c = oppNew.Id;
tickets.add(et1);
E_Ticket__c et2 = new E_Ticket__c();
et2.Airline__c = 'Virgin Atlantic';
et2.Opportunity__c = oppNew.Id;
tickets.add(et2);
// ...
// add as many tickets needed for all Airlines
// ...
insert tickets;
update tickets;
}
}
Also, make sure custom objects match in both your sandbox and deployment/production server.
In order to get 100% code coverage, you'd need to write a test for each "else if" statement.
You currently only have one test, for one "else if."

Resources